Firestore - 如何从值映射 add/subtract
Firestore - how to add/subtract from a map of values
我正在按照 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);
});
是否有更简单的方法 - 我是否遗漏了什么?
首先,您不能对对象使用 push 方法,因为映射不是数组。
您可以简单地使用 .
或 []
运算符来 access/add/update JS 中映射的值。
对于像数组和对象这样存储在 firestore 中的对象,您不能直接 "push" 给它们赋值。您首先需要获取包含它们的文档,然后在本地更新它们的值。
完成后,将值更新到 Firestore。
为了简化流程,您可以使用 Firestore SDK 提供的 runTransaction()
方法,如果您使用 Cloud Functions,则可以使用 Admin SDK。
这是可以为您完成工作的代码。
const docRef = this.afs.doc(`groups/${groupId}`);
db.runTransaction((t) => { // db is the firestore instance
return t.get(docRef).then((doc) => { // getting the document from Firestore
// {} is a fallback for the case if the "obj" is not present in the firestore
const obj = doc.get("contacts") ? doc.get("contacts") : {};
obj[contactId] = true; // updating the value here locally
t.set(docRef, { contacts: obj }, { // updating the value to Firestore.
merge: true,
});
return;
}).then((result) => {
console.log('map updated', result);
return;
}).catch((error) => handleError(error));
});
只需推入地图
使用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!");
});
我正在按照 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);
});
是否有更简单的方法 - 我是否遗漏了什么?
首先,您不能对对象使用 push 方法,因为映射不是数组。
您可以简单地使用 .
或 []
运算符来 access/add/update JS 中映射的值。
对于像数组和对象这样存储在 firestore 中的对象,您不能直接 "push" 给它们赋值。您首先需要获取包含它们的文档,然后在本地更新它们的值。
完成后,将值更新到 Firestore。
为了简化流程,您可以使用 Firestore SDK 提供的 runTransaction()
方法,如果您使用 Cloud Functions,则可以使用 Admin SDK。
这是可以为您完成工作的代码。
const docRef = this.afs.doc(`groups/${groupId}`);
db.runTransaction((t) => { // db is the firestore instance
return t.get(docRef).then((doc) => { // getting the document from Firestore
// {} is a fallback for the case if the "obj" is not present in the firestore
const obj = doc.get("contacts") ? doc.get("contacts") : {};
obj[contactId] = true; // updating the value here locally
t.set(docRef, { contacts: obj }, { // updating the value to Firestore.
merge: true,
});
return;
}).then((result) => {
console.log('map updated', result);
return;
}).catch((error) => handleError(error));
});
只需推入地图
使用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!");
});