node.js - FireBase 错误解析错误 : Unexpected token return

标签 node.js firebase google-cloud-functions

我正在尝试执行推送通知。因此,当我在 firebase 上部署代码时,当我尝试上传 firebase 函数时,出现以下错误

这是我的终端中显示的错误:

   22:14  warning  Avoid nesting promises                      promise/no-nesting
   22:69  error    Each then() should return a value or throw  promise/always-return

   ✖ 2 problems (1 error, 1 warning)

   npm ERR! code ELIFECYCLE
   npm ERR! errno 1
   npm ERR! functions@ lint: `eslint .`
   npm ERR! Exit status 1
   npm ERR! 
   npm ERR! Failed at the functions@ lint script.
   npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

   npm ERR! A complete log of this run can be found in:
   npm ERR!     /Users/mac/.npm/_logs/2018-05-17T17_45_54_673Z-debug.log

   Error: functions predeploy error: Command terminated with non-zero exit code1

JavaScript 代码:

'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification =functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
        const user_id = context.params.user_id;
        const notification = context.params.notification;
        console.log('The User Id is : ', user_id);
        if(!event.data.vall()){
            return console.log('A notification has been deleted from the database : ',notification_id);
        }
        const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
            return deviceToken.then(result =>{
                        const token_id = result.val();
                        const payload = {
                            notification: {
                                title : "Friend Request",
                                body : "You have received a new Friend Request",
                                icon : "default"
                            }
                        };
             return admin.messaging().sendToDevice(token_id, payload).then(response =>{
                console.log("This was the notification Feature");
                    });
        });
});

如何解决?

最佳答案

您有两个返回:

返回 return admin.messaging().sendToDevice(token_id, Payload).then(response =>{ console.log("这是通知功能"); });

替换:

return admin.messaging().sendToDevice(token_id, payload).then(response =>{
            console.log("This was the notification Feature");
                });

关于node.js - FireBase 错误解析错误 : Unexpected token return,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397529/

相关文章:

javascript - 给定 url,如何加载图像,然后将其转换为 Javascript 中的 tf.tensor 数据?

android - 多个开发人员如何在连接到单个 Firebase 控制台的同一个 Android 应用程序上工作?

android - Firebase 连接检测在 60 秒后不起作用

reactjs - 在 React.JS 中刷新后,如何让用户保持登录到 firebase 的状态?

javascript - 无法读取未定义的属性 'val'

reactjs - React Hooks 不适用于 Firebase Cloud Functions 错误 : Invariant Violation: Invalid hook call

javascript - 在异步模式下使用时,redis createClient 上的 Google 云函数错误

javascript - 尝试避免在没有回调 hell 的快速路由中使用匿名函数

javascript - 在nodejs/events.js的emitMany函数中克隆listeners数组的原因是什么?

javascript - 如何在保留数据的同时链接 Promises?