云firebase批量更新
我正在尝试使用批更新来更新每个快照中的计数。但是看起来这个函数甚至没有运行。我知道这与第二个承诺有关,但我不确定在哪里云firebase批量更新,firebase,promise,google-cloud-firestore,google-cloud-functions,Firebase,Promise,Google Cloud Firestore,Google Cloud Functions,我正在尝试使用批更新来更新每个快照中的计数。但是看起来这个函数甚至没有运行。我知道这与第二个承诺有关,但我不确定在哪里 import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; export const replyCreated = functions .firestore .document(`/Reply/{replyId}`) .onCre
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
export const replyCreated = functions
.firestore
.document(`/Reply/{replyId}`)
.onCreate((change: any, context: functions.EventContext) => {
const promises = [];
promises.push(admin.firestore().doc(`Challenge/${change.data().challenge_id}`).update({replyCount: admin.firestore.FieldValue.increment(1)}))
promises.push(admin.firestore()
.collection(`User`)
.where('following', 'array-contains', change.data().user_id).get().then((snapshot: any) => {
if (!snapshot.empty) {
const batch = admin.firestore().batch();
snapshot.forEach((doc: any) => {
const tempObject = doc.data()
console.log(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)
const myChallenge = admin.firestore().doc(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)
batch.update(myChallenge, {replyCount: admin.firestore.FieldValue.increment(1)})
})
return batch.commit().catch((err: any) => {
console.log('Batch Error', err)
});
}
else {
return Promise.resolve()
}
}))
return Promise.all(promises)
.then(() => {
return "upvote complete";
})
})
如果我正确理解您的代码,您不需要使用
Promise.all()
,但需要正确链接异步Firestore方法返回的不同承诺
以下方法可以实现此目的(未经测试):
如果需要并行执行许多异步方法(返回承诺),可以使用
Promise.all()
。在您的情况下(如果我没有弄错的话),您需要并行执行异步方法的唯一情况是在使用批处理写入的块中,因此并行执行是由批处理写入本身执行的。对于其他方法,它更像是一个顺序执行,您必须用该方法链接承诺。如果我正确理解您的代码,您不需要使用Promise.all()
,但您需要正确链接异步Firestore方法返回的不同承诺
以下方法可以实现此目的(未经测试):
如果需要并行执行许多异步方法(返回承诺),可以使用
Promise.all()
。在您的情况下(如果我没有弄错的话),您需要并行执行异步方法的唯一情况是在使用批处理写入的块中,因此并行执行是由批处理写入本身执行的。对于其他方法,它更像是一个顺序执行,您必须将承诺与方法联系起来
export const replyCreated = functions
.firestore
.document(`/Reply/{replyId}`)
.onCreate((change: any, context: functions.EventContext) => {
return admin.firestore().doc(`Challenge/${change.data().challenge_id}`).update({ replyCount: admin.firestore.FieldValue.increment(1) })
.then(() => {
return admin.firestore()
.collection(`User`)
.where('following', 'array-contains', change.data().user_id).get()
})
.then((snapshot: any) => {
if (!snapshot.empty) {
const batch = admin.firestore().batch();
snapshot.forEach((doc: any) => {
const tempObject = doc.data()
console.log(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)
const myChallenge = admin.firestore().doc(`/Subscribed_Challenges/${tempObject.userId}/myChallenges/${change.data().challenge_id}`)
batch.update(myChallenge, { replyCount: admin.firestore.FieldValue.increment(1) })
})
return batch.commit()
}
else {
throw new Error('Snapshot empty')
}
})
.catch((err: any) => {
console.log('Error', err);
return null;
});
})