如何修复每个then()应该返回一个值或抛出

时间:2019-02-16 11:13:25

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

我正在尝试为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

任何帮助都会非常有用:)

1 个答案:

答案 0 :(得分:0)

前两个警告是由以下行引起的:

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。我建议您针对收到的每种警告/错误查找说明,并尝试自己先进行修复,因为这样做可以防止被低估。