gpt4 book ai didi

javascript - Firebase 部署函数发送推送通知时出错

转载 作者:数据小太阳 更新时间:2023-10-29 05:59:54 24 4
gpt4 key购买 nike

我正在开发一个 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

关于javascript - Firebase 部署函数发送推送通知时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com