我正在构建一个Firebase云功能,当我更改数据库中的值时,我会获得数据库中所有用户的UID,然后检查一个名为wishlists的节点,以查看该用户的愿望清单中是否有产品。如果用户的愿望清单没有产品,则云功能会向用户发送一条带有味精的消息,以填写其愿望清单。该功能仅适用于一位用户,但是当我迭代该用户的节点并调用我为一位用户所做的工作时,它会在完成所有工作之前完成。我相信问题的出处在于未正确处理异步功能。任何帮助表示赞赏。谢谢!
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const runtimeOpts = {
timeoutSeconds: 180,
memory: '1GB'
}
admin.initializeApp(functions.config().firebase);
exports.sendAdminNotificationForEmptyWishlists = functions.runWith(runtimeOpts)
.database.ref('/dev/fireNotifCloudFunc')
.onWrite(async () => {
// get all users uids
const ref = admin.database().ref().child('Influencers')
return ref.once("value")
.then(function(snapshot) {
snapshot.forEach( async function(child) {
await checkUserWishlists(child.key)
})
})
.catch(function(error){
console.log(error);
});
});
//check users wishlists
async function checkUserWishlists(uid){
var ref = admin.database().ref().child('Articles').child(uid)
ref.once("value")
.then(function(snapshot) {
if(snapshot.exists()){
const wishlistsNumber = snapshot.numChildren();
let wishlistCount = 0
let userHasProduct = false
var BreakException = {};
try {
snapshot.forEach(function(child) {
wishlistCount = wishlistCount + 1
//wishlist node
if(child.child("articles").exists()){
//user have not empty wishlist
userHasProduct = true
throw BreakException
}
});
}
catch(err){
if (err !== BreakException) throw err;
}
if((wishlistCount === wishlistsNumber) && !userHasProduct){
let fcmToken = (async () => {
fcmToken = await getUserFirestoreFCM(uid).then(()=>{
sendPushNotificationToUser(fcmToken,"emptyWishlists")
return fcmToken
})
.catch((error)=>{
console.log("error getting fcm token " + error);
})
})();
}
}
else{
console.log("user did not exist in articles node" );
return
}
})
.catch(function(error){
console.log(error);
});
}
async function getUserFirestoreFCM(uid){
let documentRef = admin.firestore().doc("users_table/" + uid);
documentRef.get().then(documentSnapshot => {
if (documentSnapshot.exists) {
console.log('Document exist')
return documentSnapshot.get('fcmToken')
}
else {
console.log('Document dosen\'t exist')
return
}
})
.catch(function(error){
console.log('error geting fcm token from firestore ' + error);
})
}
async function sendPushNotificationToUser(fcm,type){
//get fcm token
if(type === "emptyWishlists"){
//notification content
const payloadEmptyWishlists = {
notification: {
title: `Vos wishlists sont vides !`,
body: "Hello veuillez ajouter des produits dans vos wishlists pour joindre les
influenceurs dans le fil d'acceil, cheers ! ",
icon: "default",
sound: "default"
}
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
//send notif
return admin.messaging().sendToDevice(fcm,
payloadEmptyWishlists,options).then(function(response){
console.log('Notification sent successfully:',response);
return
})
.catch(function(error){
console.log('Notification sent failed:',error);
});
}
}
答案 0 :(得分:0)
我认为您不应该在async/await
循环回调中使用forEach
。这通常是棘手的。在寻找良好的解释后,我发现了这个article。我认为它非常简短。
在这种情况下,我认为您不需要这个async/await
,但是为了确保确实需要实现它。无论如何,如果您似乎仍然需要异步,则在上述文章中有解决方法。