【发布时间】:2019-11-13 06:22:34
【问题描述】:
我有一组为用户映射的订单。这是users/{userID}/orders/{orderID}) 格式。我需要一个函数,该函数的 onUpdate 会向保存在 users/{userID}/tokens 中的令牌数组发送通知
exports.modifyUserCart = functions.firestore
.document('users/{userID}/orders/{orderID}')
.onUpdate((change, context) => {
const document = change.after.exists ? change.after.data() : null;
console.log(document.order_id)
// document.order_id . "This prints correctly"
// The tokens to be added to an array are in (users/{userID}/tokens). How
// do I get the tokens from the collection of tokens
var tokens = [] //array of tokens
var message = {
notification: {
title: 'Get an upfront discount',
body: "Clear your items in cart in the next hour to get an upfront
discount of $100"
},
token: tokens
};
admin
.messaging()
.send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
// perform desired operations ...
});
【问题讨论】:
-
我已经用实际代码编辑了帖子。
-
您应该查看firestore admin api 和the documentation 中的Node.js 查询示例。这可用于从云函数内部进行查询。 This question 的情况与您的情况非常相似,可能会有用。确保您还小心返回承诺链(例如,您当前的消息调用没有这样做)。
-
您是否收到错误消息,如果您收到错误消息,能否与我们分享。如果您遇到错误,您知道是在阅读时还是在写作时出错?
标签: firebase google-cloud-firestore google-cloud-functions