Javascript 如何在Firebase中动态更新集合中的文档
我已经能够在Firebase中动态创建记录(用户配置文件)并检索它们的唯一ID 但现在我希望能够让最终用户更新他们的个人资料。虽然我可以检索文档ID,也就是配置文件的uid。如何使用模板实现动态获取登录者的uid并更新特定记录的功能 我尝试了以下方法:Javascript 如何在Firebase中动态更新集合中的文档,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我已经能够在Firebase中动态创建记录(用户配置文件)并检索它们的唯一ID 但现在我希望能够让最终用户更新他们的个人资料。虽然我可以检索文档ID,也就是配置文件的uid。如何使用模板实现动态获取登录者的uid并更新特定记录的功能 我尝试了以下方法: async updateProfile() { const docRef = await db.collection("users").get(`${this.userId}`); docRef.update({ o
async updateProfile() {
const docRef = await db.collection("users").get(`${this.userId}`);
docRef.update({
optInTexts: this.form.optInTexts,
phone: this.form.mobile
});
db.collection("users")
.update({
optInTexts: this.form.optInTexts,
phone: this.form.mobile
})
.then(function () {
console.log("Profile successfully updated!");
})
.catch(function (error) {
console.error("Error updating document: ", error);
});
}
`
我也试过了
async updateProfile() {
const docRef = await db.collection("users").where("userId", "==", `${this.userId}`);
docRef.update({
optInTexts: this.form.optInTexts,
phone: this.form.mobile
});
db.collection("users")
.update({
optInTexts: this.form.optInTexts,
phone: this.form.mobile
})
.then(function () {
console.log("Profile successfully updated!");
})
.catch(function (error) {
console.error("Error updating document: ", error);
});
}
``
还有这个
async updateProfile() {
const docRef = await db.collection("users").get(`${this.userId}`);
docRef.update({
optInTexts: this.form.optInTexts,
phone: this.form.mobile
});
db.collection("users/`${this.userId}`")
.update({
optInTexts: this.form.optInTexts,
phone: this.form.mobile
})
.then(function () {
console.log("Profile successfully updated!");
})
.catch(function (error) {
console.error("Error updating document: ", error);
});
}
错误
docRef.update不是一个函数
在查看此帖子后,我能够解决此问题:
我试图引用的集合是一个用户表,其中包含从Firebase现成的Google auth创建的数据。因此,查询和更新数据不同于我的其他数据,其中文档ID等于集合的uid。工作解决方案:
async updateProfile() {
const e164 = this.concatenateToE164();
const data = {
optInTexts: this.form.optInTexts,
phone: e164};
const user = await db.collection('users')
.where("uid", "==", this.userId)
.get()
.then(snapshot => {
snapshot.forEach(function(doc) {
db.collection("users").doc(doc.id).update(data);
});
})
}
get()不返回文档引用。它返回一个文档快照。我认为您可以通过查看文档中的示例来更好地了解get()的工作原理。