[英]Node.js Use Promise.all For returning asynchronous call?
目前,我正在使用雲功能來查詢帖子。 當查詢帖子時,我在查詢帖子的一些其他更新后為firebase設置了一些更新,例如:
const getPostsForDate = admin.firestore().collection('posts').where('timeOfDeletion', '<', currentTime)
return getPostsForDate.get().then(snapshot => {
const updates = {}
var counter = 0
const batch = admin.firestore().batch()
snapshot.forEach((doc) => {
var key = doc.id
admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
if (snapshot.exists()) {
const convoIDCollection = snapshot.val()
for (var child in convoIDCollection) {
console.log(child)
updates["conversations/" + child] = null
updates["messages/"+ child] = null
updates["convoID/"+ child] = null
}
}
updates["/convoID/" + key] = null
updates["/reveals/" + key] = null
updates["/postDetails/" + key] = null
const postFireStoreRef = admin.firestore().collection('posts').doc(key)
const posterRef = admin.firestore().collection('posters').doc(key)
batch.delete(postFireStoreRef)
batch.delete(posterRef)
counter++
})
})
if (counter > 0) {
console.log("at the deletion")
return Promise.all[admin.database().ref().update(updates), batch.commit()]
}
else {
console.log("null")
return null
}
})
})
但是問題是查詢admin.database()。ref('convoID / ...)是異步的; 因此,更新將空發送到數據庫,並且沒有任何更改。 現在,解決這個問題的方法就是promise,除了實現promise。所有其他回報都不如預期。 我努力了
var promises = []
snapshot.forEach((doc) => {
var key = doc.id
promises.push(admin.database().ref('/convoID/' + key).once('value', (snapshot) => {
if (snapshot.exists()) {
const convoIDCollection = snapshot.val()
for (var child in convoIDCollection) {
console.log(child)
updates["conversations/" + child] = null
updates["messages/"+ child] = null
updates["convoID/"+ child] = null
}
}
updates["/convoID/" + key] = null
updates["/reveals/" + key] = null
updates["/postDetails/" + key] = null
const postFireStoreRef = admin.firestore().collection('posts').doc(key)
const posterRef = admin.firestore().collection('posters').doc(key)
batch.delete(postFireStoreRef)
batch.delete(posterRef)
counter++
})
)
})
promises.all(promises).then(() =>
if (counter > 0) {
console.log("at the deletion")
return Promise.all[admin.database().ref().update(updates), batch.commit()]
}
else {
console.log("null")
return null
}
);
除了我收到錯誤的unexpected token if
, Declaration or statement expected
,最后是promises.all()這是等待異步調用完成的正確方法嗎?
為了解決這個問題,我將嘗試總結一下我們在評論中涉及的內容:
修復箭頭功能定義。 更改此:
promises.all(promises).then(() => code here)
對此:
Promise.all(promises).then(() => { code here });
並且,修復Promise.all()的調用。 更改此:
return Promise.all[...]
對此:
return Promise.all([...])
並且, admin.database().ref().once()
將返回一個promise,但.once()
是您不傳遞.once()
一個常規回調。
因此,更改此:
promises.push(admin.database().ref('/convoID/' + key).once('value', (snapshot) => { ...}));
對此:
promises.push(admin.database().ref('/convoID/' + key).once('value').then(snapshot => {...}));
而且,如果您更改此代碼的一般結構,則會更干凈:
let promises = [];
snapshot.forEach((doc) => {
promises.push(...)
});
Promise.all(promises).then(...)
對此:
Promise.all(snapshot.map(doc => {
return admin.database().ref('/convoID/' + key).once(...).then(...);
})).then(...);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.