【问题标题】:How to fix Each then() should return a value or throw如何修复每个 then() 应该返回一个值或抛出
【发布时间】:2025-12-17 05:10:01
【问题描述】:

我正在尝试为 firebase 通知构建服务器,并且我观看了一个 youtube 视频,该视频展示了如何编写该 javascript 代码,我复制并粘贴了代码,同时调整了一些路径以匹配我的 firebase 数据库,但我不知道出了什么问题,我有点不知道如何用 javascript 编写。 所以这是代码:

'use strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) => {
    const receiver_user_id = context.params.receiver_user_id;
    const notification_id = context.params.notification_id;

    console.log('We have a notification to send to :' , receiver_user_id);

    if (!data.after.val()) {
        console.log('A notification has been deleted :' , notification_id);
    }

    const sender_user_id=admin.database().ref('/Notifications/${receiver_user_id}/${notification_id}').once('value');
    return sender_user_id.then(fromUserResult => {
        const from_sender_user_id=fromUserResult.val().from;
        console.log('you have a notification from :',sender_user_id);
        const userQuery=admin.database.ref('/Users/${receiver_user_id}/messaging_token`').once('value');
        return userQuery.then(userResult => {
            const senderUserName=userResult.val();
            return null;
        });

        const DeviceToken = admin.database().ref(`/users/${receiver_user_id}/messaging_token`).once('value');

        return DeviceToken.then(result => {
            const token_id = result.val();

            const payload = {
                notification: {
                    from_sender_user_id:from_sender_user_id,
                    title: "New Chat Request",
                    body: `${senderUserName} offered you a lesson, Please Check.`,
                    icon: "default"
                }
            };

            return admin.messaging().sendToDevice(token_id, payload)
            .then(response => {
                    console.log('This was a notification feature.');
            });
        });
    });
});

这些是我得到的错误和警告:

26:44  warning  Unexpected template string expression  no-template-curly-in-string  
31:38  warning  Unexpected template string expression  no-template-curly-in-string  
32:10  warning  Avoid nesting promises                 promise/no-nesting 
38:3   error    Unreachable code                       no-unreachable 
40:10  warning  Avoid nesting promises                 promise/no-nesting

任何帮助都会非常感激:)

【问题讨论】:

  • linting 错误都是不言自明的......您是否尝试过更改代码来修复它们?
  • @CertainPerformance 如果我知道怎么做,我会的,我真的不明白失败的原因,我尝试了一些东西,但错误不断以不同的形式出现......我的意思是,我从字面上复制了教程所说的内容,但不应该出现无法访问的声明,因为它在视频中有效
  • 你有returns 后面跟着同一个块中的更多语句 - 显然,这些进一步的语句将永远不会到达,因为函数必须在那之前返回。如果视频有此代码,则视频将是错误的,遵循您的 linting 规则
  • @CertainPerformance 是的,我明白为什么它不起作用,但出于某种奇怪的原因,评论部分的每个人都非常感谢代码编写者,这意味着他们编译它时没有错误......所以我想知道您是否对它如何工作并达到以下陈述有任何建议

标签: javascript push-notification google-cloud-functions


【解决方案1】:

前两个警告是由这一行引起的:

const sender_user_id=admin.database().ref('/Notifications/${receiver_user_id}/${notification_id}').once('value');

还有这一行:

const userQuery=admin.database.ref('/Users/${receiver_user_id}/messaging_token`').once('value');

在这两种情况下,您都在字符串中使用${...},以便在构建路径时使用代码中的值。这被称为expansion of a template literal。但是您的字符串用单引号 ' 括起来,这意味着它不会被解释为模板字符串。

解决方案是在代码中使用反引号。所以:

const sender_user_id=admin.database().ref(`/Notifications/${receiver_user_id}/${notification_id}`).once('value');

还有:

const userQuery=admin.database.ref(`/Users/${receiver_user_id}/messaging_token`').once('value`);

请注意,您不应在声明 Cloud Function (exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')) 的行中使用反引号,因为该字符串实际上应该传递给 firebase-functions 模块。

有关此错误的更多信息,请参阅ESLint documentation for the error message。我建议您找到收到的每个警告/错误的解释,并尝试自己先修复它,因为这样可以防止被否决。

【讨论】:

    最近更新 更多