gpt4 book ai didi

javascript - 部署 Firebase 函数时出错 : Each then() should return a value or throw promise/always-return

转载 作者:行者123 更新时间:2023-12-02 22:12:41 24 4
gpt4 key购买 nike

我尝试过类似帖子中推荐的常见解决方案,例如返回 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);
});
}
});

最佳答案

更改此:

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);
});
}

进入此:

return 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);
});
}

此外,您在文档中缺少}:

更改: 文档('/feed/{userId}/feedItems/{activityFeedItem')

进入此:

文档('/feed/{userId}/feedItems/{activityFeedItem}')

关于javascript - 部署 Firebase 函数时出错 : Each then() should return a value or throw promise/always-return,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59509398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com