簡體   English   中英

Firebase部署功能發送推送通知時出錯

[英]Error with Firebase deploy function to send push notifications

我正在開發一個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編寫代碼。 我很感激任何建議!

改變這個:

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

你最有可能運行v1的firebase函數,這是最新版本,它給api帶來了不少變化。 您可以在此處閱讀有關更改的更多信息。 具體來說,您希望將event =>參數更改為(snap, context) =>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM