【发布时间】:2019-11-21 15:26:40
【问题描述】:
每当写入文档字段时,我都会尝试触发推送通知。由于我是 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
我没有使用承诺,因为我没有看到它的意义。但同时我尝试了它并没有帮助。
【问题讨论】:
-
如果您想用 JavaScript 为 Cloud Functions 编写有效的代码,您绝对需要了解 Promise 以及如何在各种情况下使用它们。
-
同意。我确实阅读了文档,其中大部分内容都在我脑海中浮现,所以我需要找到一个带有示例的好教程,以便我可以完全掌握这个概念。
标签: javascript node.js promise google-cloud-functions