我的函数应该截断一个Firestore集合,并在填充它之后,问题是某些文档是在删除所有文档之后写入的。我是一个异步等待的菜鸟,我的代码有问题:
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 !");
答案 0 :(得分:1)
问题夫妇
您没有将const { Crypto } = require("@peculiar/webcrypto");
const crypto = new Crypto();
const btoa = require('btoa');
const CC = "Hello World";
const b_size = CC.length;
P = new ArrayBuffer(b_size);
C = new Uint8Array(P);
function iKey() {
return crypto.subtle.importKey(
"jwk",{
kty:"RSA",
e:"AQAB",
n:"gfJ-DTivOto0B1qvI9jGQgLvPvEkfVzbhf4-GM_XGrSyJRa1ASOZV89qpBIYDq8lwSwxibybvRqROhNavH6X_xQux9TAhDVBYZv8bH7pR4cfCLQrNvbpFRan3XBv7zeifGThyJGJ8R0BJTh4R4K9cu7kE48Ig1GO6iIx6emnYaQBhRUHsLfxfhQCwlHLraOXcP_RPM9TfZRO4dVHdRrdX1B60B6OqwU1ojRmo1oLiJCN6KjwMsYbSbYnflt_uyFUGxxBQE-1qnhBkarm10pgIgVDiSIn8XKQBxLg-Ao6cc7pXytp7Bd--g45OKHinRKaSP-Ub8g3S9g4LV2Qt8UZbQ",
alg:"RSA-OAEP-256",
ext:true
},
{
name:"RSA-OAEP",
hash: {name: "SHA-256"}
},
true,
["encrypt"]
);
}
e_key = iKey();
for(i=0; i < b_size; ++i) {
C[i] = CC.charCodeAt(i);
}
function crypt() {
return crypto.subtle.encrypt(
{
name: "RSA-OAEP",
hash: {name: "SHA-256"}
},
e_key, P
).then(function(encrypted){
console.log(new Uint8Array(encrypted));
}).catch(function(err){
console.error(err);
});
}
crypt();
用于Promise.all
,因此它实际上并没有做任何事情。另外,您不会从in
该填充方法正在使用forEach,该方法不适用于Promise。更改为使用.map
类似这样的东西
for..of
答案 1 :(得分:0)
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);
});
}));