[英]Firebase Cloud Messaging - Error calling firebase.messaging()
[英]Firebase Cloud Messaging generating error
每当尝试将文档字段写入时,我都试图触发推送通知。 由于我是node.js的新手,因此调试该看似简单的函数很困难。 这是我的代码:
// 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.sendNotification = functions.firestore.document("Customer_Data/{userEmail}/Placed_Orders/{orderId}/status").onWrite(event => {
const userEmail = event.params.userEmail;
const message = "Your meal will be delivered in 2 hours";
const title = "Eat Away";
const toUser = admin.firestore().collection("Customer_Data").doc(userEmail).get();
return admin.firestore().collection("Customer_Data").doc({userEmail}).get().then(queryResult => {
const tokenId = queryResult.data().tokenId;
const notificationContent = {
notification: {
title: title,
body: message,
}
}
});
return admin.messaging().sendToDevice(tokenId , notificationContent).then(function(response) {
console.log("Message sent successfully");
}).catch(function(error){
console.log("Error sending message:", error);
});
});
这是我在终端中收到的错误:
53:11 warning Avoid nesting promises promise/no-nesting
53:11 warning Avoid nesting promises promise/no-nesting
53:77 warning Unexpected function expression prefer-arrow-callback
53:77 error Each then() should return a value or throw promise/always-return
55:13 warning Unexpected function expression prefer-arrow-callback
✖ 5 problems (1 error, 4 warnings)
0 errors and 2 warnings potentially fixable with the `--fix` option.
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.
Error: functions predeploy error: Command terminated with non-zero exit code1
我没有使用诺言,因为我没有看到诺言的意义。 但是同时我尝试了一下,但没有帮助。
没有看到诺言的要点通常是更多地了解诺言而不是不兑现诺言的原因。
您现在编写代码的方式意味着它的一部分永远不会运行。 这与Promise无关,而仅仅是因为您在代码的主要流程中有两个return语句:
exports.sendNotification = functions.firestore.document("Customer_Data/{userEmail}/Placed_Orders/{orderId}/status").onWrite(event => {
...
return admin.firestore().collection("Customer_Data").doc({userEmail}).get().then(queryResult => {
...
});
// Nothing below this line ever executes
return admin.messaging().sendToDevice(tokenId , notificationContent).then(function(response) {
console.log("Message sent successfully");
}).catch(function(error){
console.log("Error sending message:", error);
});
});
在这种情况下,您需要来自Firestore的结果来发送FCM消息,这意味着您需要在调用FCM之前await
Firestore承诺解决(使用then()
或await
):
exports.sendNotification = functions.firestore.document("Customer_Data/{userEmail}/Placed_Orders/{orderId}/status").onWrite(event => {
const userEmail = event.params.userEmail;
const message = "Your meal will be delivered in 2 hours";
const title = "Eat Away";
const toUser = admin.firestore().collection("Customer_Data").doc(userEmail).get();
return admin.firestore().collection("Customer_Data").doc({userEmail}).get().then(queryResult => {
const tokenId = queryResult.data().tokenId;
notificationContent = {
notification: {
title: title,
body: message,
}
}
return admin.messaging().sendToDevice(tokenId , notificationContent);
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.