这是一个 javascript
函数,旨在对 FireStore
执行更新,但该函数不起作用。
如果有人能看到代码中的问题,我会更高兴。
function makeUpdate(key,name) {
let theCollection = db.collection("InformationList"),
infoUnit = theCollection.doc(key).get().then(function(doc) {
if (doc.exists) {
console.log("infoUnit -name-:" + doc.get("name"));
console.log("infoUnit -telephone-:" + doc.get("telephone"));
let updateDico = {};
updateDico["name"] = name;
doc.update(updateDico);
} else {
console.log("embassyUpdate --> No such document!");
}
}).catch(err => {
console.log("Error getting documents (in makeUpdate)", err);
});
}
除了它没有执行预期的更新之外,它还在日志中打印了三条消息:
- infoUnit -name-:一些名称
- infoUnit -telephone-:一些电话号码
- 获取文档时出错(在 makeUpdate 中)
从中我可以看到,按照预期在数据库中找到了一条记录。但同时发生了未知错误。
最佳答案
doc(DocumentSnapshot 对象)上没有 update()
方法。 DocumentSnapshot 仅包含从 get()
读取的数据。如果要将数据写回到文档中,则需要使用 DocumentReference对象,可能与您调用 theCollection.doc(key)
时获得的对象相同。
关于javascript - Firestore 中的数据未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53006590/