如何更新使用火力地堡的Admin SDK在公司的FireStore用户的列表?
所以,我正在学习如何使用火力管理SDK和我遇到的是我不理解不足以解决问题。基本上,我询问我的数据库,并得到,我想用相同的值更新用户的一个子集。所以,我存储在每个用户的ID的,我想在一个数组更新,现在我通过阵列试图循环和更新每个用户。但我在实施这一部分问题,它与该.update(其实做)返回一个承诺,我不是100%,如何在一个循环处理这...
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp()
exports.getAllUsers = functions.https.onRequest((request, response) => {
const db = admin.firestore()
const users = db.collection('users').where('role', '==', 'artist').get()
.then(function (querySnapshot) {
//const users = querySnapshot.data()
const promises = []
querySnapshot.forEach(function (doc){
promises.push(doc)
})
return Promise.all(promises) //returning promises sends the resolved results to
}) //to the next .then()
.then(function (person){
let results = []
person.forEach(function(personSnap){
//const data = personSnap.data()
results.push(personSnap.id)
})
return results // results is the array of ids to update
})
//This .then() is where I have trouble understanding how to update
.then(function (ids){
for(var i = 0; i<ids.length; i++){
var artistsRef = db.collection('users').doc(ids[i]);
artistsRef.update({ //Should not be a return statement. This fixed my problem. Otherwise it would update the first user in the array and then leave the for loop.
'free_credits': '2'
})
}
return 'finished'
})
.then(function(reply) {
return response.send(reply)
})
.catch(function (error){
response.send(error)
})
})
我觉得我应该返回“artistsRef.update()”,因为它是一个承诺,但我认为这会导致意想不到的结果。
如果我不回的更新(),然后ESLint引发错误:“每一则()应该返回一个值或抛出”
回答如下:你回国早退最终then()
回调,发送响应之前。也许你应该再添then()
只是将结果发送任何后的最终更新完成。