angular - Firestore - 如何从值映射中添加/减去
问题描述
我正在遵循 Firestore 存储数组的说明: https ://firebase.google.com/docs/firestore/solutions/arrays
现在我想做的是推送到这张地图。例如现在我有:
Contacts
contact1: true
但我想添加或删除联系人,例如:
Contacts
contact1: true
contact2: true
我已经尝试获取联系人地图并使用推送方法,但我认为这不会起作用,因为它不是传统的数组。例如:
this.afs
.doc(`groups/${group.id}`)
.ref.get()
.then(doc => {
let contacts: Array<any> = doc.data().contacts;
contacts.push({ // error here as push is not a function
[contactId]: true
});
console.log(contacts);
});
有没有更简单的方法来做到这一点 - 我错过了什么吗?
解决方案
只需推入地图
使用update()
如下
const db = firebase.firestore();
const collection = db.collection('collectionId');
collection.doc(`groups/${group.id}`).update({
"Contacts.contact3":true
}).then(function(){
console.log("Successfully updated!");
});
推荐阅读
- java - 我正在尝试通过 ModBus RTU 协议从时间计数器 OVEN 获取数据,但收到垃圾作为响应
- python - 为什么使用 list() 构造函数?
- php - 从 nuxt.js 发布到 laravel 后,值变为空
- wagtail - 如何在 InlinePanel 中设置初始空(可订购)项目的数量?
- javascript - 提交后刷新对话框及其内容
- reactjs - 使用 BrowserRouter 单击按钮/链接后如何切换到其他 url
- javascript - 为什么在月份下拉菜单中选择选项 12 时仅不加载 12 月份?
- python - 如何调出类对象?
- c# - LINQ where 子句返回 null
- javascript - 如何将 props 和 function 传递给 react-widow 列表?