【发布时间】:2019-12-28 08:49:54
【问题描述】:
我已经尝试过类似帖子中推荐的常见解决方案,例如返回 null 或重新调整非 null 值,但这只会导致另一个部署错误,如下所示:
未能配置触发器提供程序/cloud.firestore/eventTypes/document.create@firestore.googleapis.com....
我也在使用 eslint。我的功能代码如下:
exports.onCreateActivityFeedItem = functions
.firestore
.document('/feed/{userId}/feedItems/{activityFeedItem')
.onCreate(async (snapshot, context) => {
console.log('Activity Feed Item Create', snapshot.data());
// 1) Get user connected to the feed
const userId = context.params.userId;
const userRef = admin.firestore().doc(`users/${userId}`);
const doc = await userRef.get();
// 2) Once we have user, check if the have notification token
//send notification if they have a token
const androidNotificationToken = doc.data().androidNotificationToken;
const createdActivityFeedItem = snapshot.data();
if(androidNotificationToken){
//send the notification
sendNotification(androidNotificationToken, createdActivityFeedItem);
}else{
console.log("No token for user, cannot send notification");
}
function sendNotification(androidNotificationToken, activityFeedItem){
let body;
// 3) switch body value based off of notification type
switch (activityFeedItem.type){
case "comment":
body = `${activityFeedItem.username} replied: ${activityFeedItem.commentData}`;
break;
case "like":
body = `${activityFeedItem.username} liked your post`;
break;
case "follow":
body = `${activityFeedItem.username} started following your pet`;
break;
default:
break;
}
// 4) Create message for push notification
const message = {
notification: { body },
token: androidNotificationToken,
data: { recipient: userId }
};
// 5) Send message with admin.messaging()
admin
.messaging()
.send(message)
.then(response => {
// Response is a message ID string
console.log("Successfully sent message", response);
//return 1; //Last edition trying to fix bug
}).catch(error => {
console.log("Error sending message", error);
});
}
});
【问题讨论】:
-
请将您的问题缩小到一个问题。您在这里引用了两个问题,一个与 eslint 有关,另一个与触发器的创建有关。这两个问题可能彼此无关。如果你创建一个 MCVE 会很有帮助:stackoverflow.com/help/minimal-reproducible-example
标签: javascript firebase google-cloud-firestore eslint