Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何修复每个then()应该返回一个值或抛出_Javascript_Push Notification_Google Cloud Functions - Fatal编程技术网

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

Javascript 如何修复每个then()应该返回一个值或抛出,javascript,push-notification,google-cloud-functions,Javascript,Push Notification,Google Cloud Functions,我正在尝试为firebase通知构建服务器,我观看了一段youtube视频,该视频演示了如何编写javascript代码,我复制并粘贴了代码,同时调整了一些路径以匹配firebase数据库,但我不知道出了什么问题,也不知道如何使用javascript编写。 这就是代码: 'use strict' const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.init

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

任何帮助都将非常感谢:

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

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');
这一行:

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');
在这两种情况下,您都在字符串中使用${…},以便在构建路径时使用代码中的值。这被称为。但是字符串用单引号括起来,这意味着它不会被解释为模板字符串

解决方案是在代码中使用反勾号。因此:

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

注意,您不应该在声明云函数exports.sendNotification=functions.database.ref'/Notifications/{receiver\u user\u id}/{notification\u id}的行中使用反勾号,因为该字符串实际上应该传递给firebase函数模块


有关此错误的详细信息,请参阅。我建议您找到每个警告/错误的解释,并尝试首先自己修复,因为这将防止被否决。

所有的林廷错误都是不言自明的。。。你试过修改代码来修复它们吗?@CertainPerformance如果我知道怎么做,我会的,我真的不知道失败的原因,我试过一些东西,但错误以不同的形式不断出现。。。我的意思是,我从文字上复制了教程所说的内容,但不应该出现“无法访问”语句,因为它在视频中起作用。在同一块中,返回的语句后面是其他语句——显然,这些进一步的语句永远不会到达,因为函数必须在此之前返回。如果视频中有此代码,则视频将是错误的rules@CertainPerformance是的,我理解它为什么不起作用,但出于某种奇怪的原因,注释部分的每个人都非常感谢代码编写者,这意味着他们编译了它,但没有错误。。。因此,我想知道你是否有任何建议,它将如何工作,并达成以下声明