[英]chaining functions one after the other with async await
My functions should truncate a firestore collection and after populate it, the problem is that some documents are writtened after before all documents are deleted.我的函数应该截断一个 firestore 集合,并在填充它之后,问题是在删除所有文档之前写入了一些文档。 I'm a noob with async await there is something wrong in my code :
我是 async await 的菜鸟,我的代码有问题:
let db = admin.firestore();
let truncateCollection = async function () {
const snapshot = await db.collection(collectionToPopulate).get();
await snapshot.docs.map(function (doc) {
db.collection(collectionToPopulate).doc(doc.id).delete().then(function () {
console.log("document supprimé : "+doc.id);
}).catch(function (error) {
console.error("erreur de suppression de document",error);
});
});
await populate();
};
let populate = async function () {
jsonInput.forEach(function (obj, index) {
Object.entries(obj).forEach(([key,value]) => {
//console.log(value);
db.collection(collectionToPopulate).doc(key).set({
name: value.name,
imageUrl: value.image_url,
}).then(function (docRef) {
console.log("Document written with ID: ", key);
}).catch(function (error) {
console.error("Error adding document: ", error);
});
});
});
};
truncateCollection();
res.send("Job Done !");
Couple of problems几个问题
You are not using Promise.all
for .map
so it is not really doing anything.您没有将
Promise.all
用于.map
所以它实际上并没有做任何事情。 Also you are not returning the promise from with in你也没有从 with in 返回承诺
THe populate method is using forEach which doesn't work with promises. populate 方法正在使用 forEach,它不适用于承诺。 Change that to use
for..of
将其更改为用于
for..of
Something like this像这样的东西
const db = admin.firestore();
// eslint-disable-next-line func-style
const truncateCollection = async function () {
const snapshot = await db.collection(collectionToPopulate).get();
await Promise.all(snapshot.docs.map(function (doc) {
return db.collection(collectionToPopulate).doc(doc.id).delete().then(function () {
console.log(`document supprimé : ${doc.id}`);
}).catch(function (error) {
console.error("erreur de suppression de document", error);
});
}));
await populate();
};
// eslint-disable-next-line func-style
const populate = async function () {
for (const obj of jsonInput) {
for (const [key, value] of Object.entries(obj)) {
try {
// eslint-disable-next-line no-await-in-loop
const response = await db.collection(collectionToPopulate).doc(key).set({
"name": value.name,
"imageUrl": value.image_url
});
console.log("Document written with ID: ", key);
} catch(err) {
console.error("Error adding document: ", error);
}
}
}
};
truncateCollection();
res.send("Job Done !");
docs.map()
does not return a promise that resolves when all asynchronous deletions have been completed. docs.map()
不会返回在所有异步删除完成后解决的承诺。 Instead return
the promise returned by delete().then().catch()
and apply a Promise.all
on the return value of docs.map()
:而是
return
返回的承诺delete().then().catch()
并应用Promise.all
上的返回值docs.map()
await Promise.all(snapshot.docs.map(function (doc) {
return db.collection(collectionToPopulate).doc(doc.id).delete().then(function () {
console.log("document supprimé : "+doc.id);
}).catch(function (error) {
console.error("erreur de suppression de document",error);
});
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.