【发布时间】:2019-11-26 08:56:32
【问题描述】:
将文档添加到 Firestore 时,我尝试将其转移到另一个名称并删除旧文档。我不明白该怎么做。似乎有可能以某种方式设置触发器来执行 firebase 函数中的特定函数?
为了更改文档的名称,我尝试从另一个文档中提取文档的数量,但似乎我弄错了。
exports.changeLocation = functions.region('europe-west1').firestore.document('users/{userId}/notes/{noteId}').onCreate((snap,context) => {
const uid = context.params.userId;
const data = snap.data();
let count;
let documentRef = db.collection("users").doc(uid).collection("info").doc("info").get().then(doc => {
if(!doc.exists){
console.log("No such document");
}else{
count = doc.id("countMutable").toString();
}
}).catch(err => {
console.log("Error getting document: ", err);
});
const p2 = documentRef.delete();
return db.collection("users").doc(uid).collection("notes").doc("its_notes:"+count).set(data);
});
【问题讨论】:
-
有点难以理解你到底想要做什么......你真的有一些像
"its_notes:" + count这样的文档吗?您能否制作图表或在项目符号列表中解释您的用例的确切流程。哪个doc保存数据,哪个要更新,哪个要删除。另请注意,您需要链接异步方法返回的 Promises。我建议您观看 Firebase 视频系列中有关“JavaScript Promises”的 3 个视频:firebase.google.com/docs/functions/video-series
标签: firebase google-cloud-firestore google-cloud-functions