作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 React Native 中执行类似于以下代码的操作:
await firestore()
.collection('Users')
.doc('ABC')
.update({
this.state.documentName: url,
});
但我需要 this.state.documentName
是动态的,但 VSCode 报告错误,提示 ':' expected.
。我错过了什么?
最佳答案
您非常接近,为了计算字段名称,将其括在方括号中:
await firestore()
.collection('Users')
.doc('ABC')
.update({
[this.state.documentName]: url,
});
update()
方法还接受键值对中的参数:
await firestore()
.collection('Users')
.doc('ABC')
.update(this.state.documentName, url);
注意:您应该确保使用适当的 security rules如果该信息也包含在他们的用户文档中,则可以防止用户将自己提升为管理员。
关于javascript - 如何更新 Firebase Firestore 中的动态字段名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70737143/
我是一名优秀的程序员,十分优秀!