代码之家  ›  专栏  ›  技术社区  ›  Simon74

NodeJs代码在尝试发送云通知时显示错误

  •  0
  • Simon74  · 技术社区  · 8 年前

    我是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错误!代码ELIFECYCLE

    npm错误!错误1

    npm错误!功能@lint: eslint .

    npm错误!退出状态1

    npm错误!

    npm错误!在函数@lint脚本中失败。

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

    我的ide括号显示第20、70行有错误。我想不出是什么问题。

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

    提前感谢!

    1 回复  |  直到 8 年前
        1
  •  0
  •   Marcos Casagrande    8 年前

    您得到的错误是 eslint 错误,如果您运行代码,它将正常工作,因为它是有效的javascript。

    林特法则 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);
            });
    });
    

    使用 异步/等待 (您需要先传输代码)

    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);
        }
    });