每个then()都应该在Firebase cloud函数中返回一个值或抛出错误



我正在使用javascript为Firebase编写一个云函数,而我部署了一个发生错误并终止部署过程的函数。

错误:每个 then() 应该返回一个值或抛出

如何解决此错误?

下面我附上了我的代码。

exports.sendNotificationForLikeOrFollow = functions.database.ref('/USER_MANAGEMENT/USER_ACTIVITY/{activityId}').onCreate((snap, context) => {
var type = snap.val().ACTIVITY_TYPE;
if (type=='Comment') {
return; //There is a separate function for comment notification
}
if (type=='Like') {
const likeUserId = snap.val().BY_USER_NODE_NAME;
const publishedUserId = snap.val().PUBLISHED_USER_NODE_NAME;
const puplishedContentId = snap.val().LIKE_PUBLISHED_CONTENT_NODE_NAME;
const likedUserName = snap.val().BY_USER_NAME;
const likedUserPhotoUrl = snap.val().BY_USER_PHOTO_URL;
// var publishedUserRef = event.data.ref.parent.parent.child('USERS/'+publishedUserId);
// var likedUserRef = event.data.ref.parent.parent.child('USERS/'+likeUserId);
var publishedUserRef = db.ref("USER_MANAGEMENT/USERS/"+publishedUserId);
var likedUserRef = db.ref("USER_MANAGEMENT/USERS/"+likeUserId);
return Promise.all([publishedUserRef.once('value')]).then(function(snaps) {
var data = snaps[0].val();
const fcmToken = data.FCM_TOKEN;
// Notification details.
const payload = {
notification: {
title: '❤️ You got a new like!',
body: likedUserName+` liked your artwork.`,
sound: 'default',
icon: '',
byUserId: likeUserId,
byUserName: likedUserName,
type: 'Like',
likedPuplishedContentId: puplishedContentId,
publishedUserId: publishedUserId,
byUserPhotoUrl: likedUserPhotoUrl,
badge : '1'
}
};
// Listing all tokens.
const tokens = fcmToken;
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
response.results.forEach((result, index) => {
console.log('FcmToken: ', tokens);
const error = result.error;
if (error) {
console.log('Error Occured:', error);
}
});
console.log('User Liked');
});
});
}
if (type=='Follow') {
const followerUserId = snap.val().BY_USER_NODE_NAME;
const followeeUserId = snap.val().FOLLOWING_USER_NODE_NAME;
const followerUserName = snap.val().BY_USER_NAME;
const followerPhotoUrl = snap.val().BY_USER_PHOTO_URL;
// var followerUserRef = event.data.ref.parent.parent.child('USERS/'+followerUserId);
// var followeeUserRef = event.data.ref.parent.parent.child('USERS/'+followeeUserId);
var followerUserRef = db.ref('USER_MANAGEMENT/USERS/'+followerUserId);
var followeeUserRef = db.ref('USER_MANAGEMENT/USERS/'+followeeUserId);
var isFollow;
//const followeeFollowingRef = event.data.ref.parent.parent.child('FOLLOWING/'+followeeUserId+'/'+followerUserId);
const followeeFollowingRef = db.ref('USER_MANAGEMENT/FOLLOWING/'+followeeUserId+'/'+followerUserId);
return Promise.all([followeeUserRef.once('value')]).then(function(snaps) {
var data = snaps[0].val(); // Get whole USER_MANAGEMENT snapshot
const fcmToken = data.FCM_TOKEN
followeeFollowingRef.once('value').then(function(followeeSnapshot) {
if (followeeSnapshot.exists()) {
isFollow = 'YES';
console.log('FOLLOW YES');
}else{
isFollow = 'NO';
console.log('FOLLOW NO');
}
// Notification details.
const payload = {
notification: {
title: '   You have a new follower!',
body: followerUserName+` is now following you.`,
sound: 'default',
icon: '',
byUserId: followerUserId,
byUserName: followerUserName,
type: 'Follow',
isFollow: isFollow,
byUserPhotoUrl: followerPhotoUrl,
badge : '1'
}
};
// Listing all tokens.
const tokens = fcmToken;
console.log('FcmToken: ', tokens);
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.log('Error Occured:', error);
}
});
console.log('User Followed');
});
});    
});
}
});

此功能在几年前就已经部署完毕,并且工作正常。但是现在我尝试将相同的代码部署到另一个由于上述错误而停止的项目。

您没有正确链接 Firebase 异步方法返回的不同承诺。

另外,请注意,您不需要使用Promise.all(),因为once()方法只返回一个 Promise。同样,您应该正确链接Promises,而不是使用Promise.all(),后者应该用于并行执行异步方法,而不是按顺序执行。

因此,以下内容应该可以解决问题(未经测试):

exports.sendNotificationForLikeOrFollow = functions.database.ref('/USER_MANAGEMENT/USER_ACTIVITY/{activityId}').onCreate((snap, context) => {
var type = snap.val().ACTIVITY_TYPE;
if (type == 'Comment') {
return null; //There is a separate function for comment notification
}
if (type == 'Like') {
const likeUserId = snap.val().BY_USER_NODE_NAME;
const publishedUserId = snap.val().PUBLISHED_USER_NODE_NAME;
const puplishedContentId = snap.val().LIKE_PUBLISHED_CONTENT_NODE_NAME;
const likedUserName = snap.val().BY_USER_NAME;
const likedUserPhotoUrl = snap.val().BY_USER_PHOTO_URL;
var publishedUserRef = db.ref("USER_MANAGEMENT/USERS/" + publishedUserId);
return publishedUserRef.once('value')
.then(snaps => {
var data = snaps[0].val();
const fcmToken = data.FCM_TOKEN;
// Notification details.
const payload = {
notification: {
title: '❤️ You got a new like!',
body: likedUserName + ` liked your artwork.`,
sound: 'default',
icon: '',
byUserId: likeUserId,
byUserName: likedUserName,
type: 'Like',
likedPuplishedContentId: puplishedContentId,
publishedUserId: publishedUserId,
byUserPhotoUrl: likedUserPhotoUrl,
badge: '1'
}
};
// Listing all tokens.
const tokens = fcmToken;
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload);
})
.then(response => {
// For each message check if there was an error.
response.results.forEach((result, index) => {
console.log('FcmToken: ', tokens);
const error = result.error;
if (error) {
console.log('Error Occured:', error);
}
});
console.log('User Liked');
return null;
});
}
if (type == 'Follow') {
// See above, it is similar
}
});

相关内容

  • 没有找到相关文章

最新更新