【问题标题】:Error with Firebase deploy function to send push notificationsFirebase 部署功能发送推送通知时出错
【发布时间】:2018-09-22 08:25:50
【问题描述】:

我正在开发一个 iOS 应用,但现在我被 Firebase 部署功能困住了。我正在尝试发送推送通知,并准备了如下代码。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.pushNotifications = functions.database.ref('/messages/{messageId}')
    .onCreate(event => {

        const data = event.data;
        const fromId = data.fromId;
        const toId = data.toId;
        const message = data.message;

        console.log(fromId + ' sent a message to' + toId);

        return admin.database().ref('/users/' + fromId).once('value', snapshot => {

            var user = snapshot.val();

            var payload = {
                notification: {
                    title: user.username,
                    body: message
                }
            }

            admin.messaging().sendToDevice(user.fcmToken, payload)
                .then(function(response) {
                    // See the MessagingDevicesResponse reference documentation for
                    // the contents of response.
                    console.log("Successfully sent message:", response);
                })
                .catch(function(error) {
                    console.log("Error sending message:", error);
                });

        })

数据库结构:

messages - messageId -fromId
                     └toId
                     └Message

         └ messageId -fromId
                     └toId
                     └Message
             .
             .
             .

这是错误信息。

37:1  error  Parsing error: Unexpected token

✖ 1 problem (1 error, 0 warnings)

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/...

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

同样在日志中,我收到如下错误:

TypeError: Cannot read property 'fromId' of undefined

发生错误是因为我没有获取 fcmToken 对吗?

我从来没有用 JavaScirpt 编码过。如有任何建议,我将不胜感激!

【问题讨论】:

  • 昨天。我对此很陌生。

标签: javascript node.js firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

改变这个:

exports.pushNotifications = functions.database.ref('/messages/{messageId}')
.onCreate(event => {

    const data = event.data;
    const fromId = data.fromId;
    const toId = data.toId;
    const message = data.message;

进入这个:

exports.pushNotifications = functions.database.ref('/messages/{messageId}')
.onCreate((snap,context) => {

    const data = snap.val();
    const fromId = data.fromId;
    const toId = data.toId;
    const message = data.message;
});

点击这里了解更多信息:

https://firebase.google.com/docs/functions/beta-v1-diff

【讨论】:

    【解决方案2】:

    您很可能正在运行 v1 的 firebase 函数,这是最新版本,它对 api 进行了相当多的更改。您可以阅读有关更改的更多信息here。具体来说,您希望将 event => 参数更改为 (snap, context) =>

    exports.dbWrite = functions.database.ref('/path').onCreate((snap, context) => {
      const data = snap.val();
      const { fromId, toId } = data;
      ...
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-09
      • 2020-11-20
      • 2018-06-01
      • 2021-03-08
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 2019-02-23
      相关资源
      最近更新 更多