【问题标题】:NodeJs Code Shows Error when trying to Send Cloud Notification尝试发送云通知时 NodeJs 代码显示错误
【发布时间】:2018-09-18 00:57:36
【问题描述】:

我是 nodeJs 的初学者,但尝试连接到云服务器以通过 firebase 云通知发送通知

   // The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from the ProjectSupporter Team");
});

exports.sendPushNotifications = functions.https.onRequest((req, res) => {
    res.send("Attempting to send push notification...")
    console.log("LOGGER --- Trying to send push message...")

    // admin.message().sendToDevice()

    var uid = 'Ky6h0BgpGDNMUyyBgE4ul99MzXk1'

    return admin.database().ref('/users/' + uid).once('value', async snapshot => {
        var user = snapshot.val();
        console.log("User username: " + user.username + " fcmToken: " + user.fcmToken);

        // See documentation on defining a message payload.
        var message = {
            notification: {
                title: "Push Notification Title",
                body: "Message Body..."
            },
          data: {
            score: '850',
            time: '2:45'
          },
          token: user.fcmToken
        };

        // Send a message to the device corresponding to the provided
        // registration token.
       try {
       const response = await admin.messaging().send(message);
       console.log('Successfully sent message:', response);
   } catch(e) {
       console.log('Error sending message:', error);
   }
});

这是我收到的错误消息:

20:70 错误解析错误:意外的令牌快照

✖ 1 个问题(1 个错误,0 个警告)

npm 错误!代码生命周期

npm 错误!错误号 1

npm 错误!函数@ lint:eslint .

npm 错误!退出状态 1

npm 错误!

npm 错误! functions@lint 脚本失败。

npm 错误!这可能不是 npm 的问题。上面可能还有额外的日志输出。

我的 ide Brackets 说第 20、70 行有一个错误。我不知道是什么问题。

这是一张图片: https://ibb.co/iBp17H

提前致谢!

【问题讨论】:

  • 请显示您遇到的错误。

标签: node.js firebase firebase-cloud-messaging


【解决方案1】:

您收到的错误是eslint 错误,如果您运行您的代码,它会起作用,因为它是有效的 javascript。

linter 规则 promise/always-return 要求您始终在承诺链中返回,即使在最后一个 .then 中也是如此。所以只需返回一些东西或使用async/await

return admin.database().ref('/users/' + uid).once('value', snapshot => {
    /*...*/
    // Send a message to the device corresponding to the provided
    // registration token.
    return admin.messaging().send(message)
        .then((response) => {
             //  promise/always-return
             console.log('Successfully sent message:', response);
             // Do something with response
             return true; // To quiet linter.
        })
        .catch((error) => {
            console.log('Error sending message:', error);
        });
});

使用async/await(您需要先转译您的代码)

Cloud Functions for Firebase Async Await style

return admin.database().ref('/users/' + uid).once('value', async snapshot => {
    /*...*/
    // Send a message to the device corresponding to the provided
    // registration token.

    try {
        const response = await admin.messaging().send(message);
        console.log('Successfully sent message:', response);
    } catch(e) {
        console.log('Error sending message:', error);
    }
});

【讨论】:

  • 在 admin.messaging 上添加返回...这将错误更改为 28:11 错误:每个 then() 都应该返回一个值或抛出 promise/always-return @marcos
  • 好吧,这不是 IDE 问题,这是你的 linter。我会在一分钟内更新我的答案
  • 现在检查我的答案,我将添加一个使用 async/await 的替代方案
  • 好吧,我认为它有效,如果它完全有效,我会将您的答案设为绿色
  • 太好了,告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 2017-02-18
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多