coo*_*nie 5 javascript node.js firebase firebase-realtime-database google-cloud-functions
我正在开发一个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);
});
})
Run Code Online (Sandbox Code Playgroud)
数据库结构:
messages - messageId -fromId
?toId
?Message
? messageId -fromId
?toId
?Message
.
.
.
Run Code Online (Sandbox Code Playgroud)
这是错误信息.
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
Run Code Online (Sandbox Code Playgroud)
同样在日志中,我得到如下错误:
TypeError: Cannot read property 'fromId' of undefined
Run Code Online (Sandbox Code Playgroud)
发生错误是因为我没有抓取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;
Run Code Online (Sandbox Code Playgroud)
进入这个:
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;
});
Run Code Online (Sandbox Code Playgroud)
点击此处了解更多信息:
https://firebase.google.com/docs/functions/beta-v1-diff
归档时间: |
|
查看次数: |
346 次 |
最近记录: |