gau*_*mar 4 transactions node.js firebase google-cloud-functions google-cloud-firestore
我正在云函数上部署此代码,获得无法修改已提交的 WriteBatch,我尝试在获取每个集合后提交,但这不是正确的方法并且不一致,尝试几个小时后无法发现错误。同样代码在冷启动后第一次运行,这篇文章有同样的问题批量写入firebase cloud firestore,在哪里创建
每组写入的新批次。在这段代码中。
var batch = db.batch();
db.collection("myposts")
.doc(post_id)
.collection("fun")
.get()
.then(snapshot => {
return snapshot.forEach(doc => {
batch.delete(doc.ref);
});
})
.then(
db
.collection("relations_new")
.where("uid", "==", uid)
.get()
.then(snapshot => {
return snapshot.forEach(doc => {
batch.delete(doc.ref);
});
})
)
.then(
db
.collection("relation")
.where("uid", "==", uid)
.get()
.then(snapshot => {
return snapshot.forEach(doc => {
batch.delete(doc.ref);
});
})
.catch(err => {
console.log(err);
})
)
.then(
db
.collection("posts")
.doc(post_id)
.get()
.then(snap => {
return batch.delete(snap.ref);
})
.then(batch.commit())
)
.catch(err => {
console.log(err);
});`
Run Code Online (Sandbox Code Playgroud)
确保从你的 then 函数返回承诺,并最终从你的云函数返回承诺。ESLint 是捕获此类错误的绝佳工具。
let batch = db.batch();
return db.collection("myposts").doc(post_id)
.collection("fun").get()
.then(snapshot => {
snapshot.forEach(doc => {
batch.delete(doc.ref);
});
return null;
})
.then(() => {
return db.collection("relations_new")
.where("uid", "==", uid)
.get();
})
.then(snapshot => {
snapshot.forEach(doc => {
batch.delete(doc.ref);
});
return null;
})
.then(() => {
return db.collection("relation")
.where("uid", "==", uid)
.get();
})
.then(snapshot => {
snapshot.forEach(doc => {
batch.delete(doc.ref);
});
return null;
})
.then(() => {
return db.collection("posts").doc(post_id).get();
})
.then(snap => {
batch.delete(snap.ref);
return null;
})
.then(() => {
return batch.commit();
})
.then(() => {
console.log("Success");
return null;
})
.catch(err => {
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4591 次 |
| 最近记录: |