如何将地图存储到 Firestore 数组
Posted
技术标签:
【中文标题】如何将地图存储到 Firestore 数组【英文标题】:How to store maps to a firestore array 【发布时间】:2019-12-31 02:03:30 【问题描述】:我在 abc 集合中有 xyz 文档。
我想将 map 添加到 document xyz 内的 an array。我可以使用此代码添加单个数组项
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.setData("data": FieldValue.arrayUnion(values));
这里的data是父数组。
但是当我尝试添加另一个 map 时,firestore 替换索引 0 的值,它 不会将地图添加到下一个索引 表示索引 1,2,3 或.....
让我们看看例子 这是地图
values =
"tokens": "99",
"title": "Cleaning",
"location": " lat": "34.4333", "lng": "53.4343",
"id": "gVnGE5ZPRnQ8HYQq7dvT",
"client": "LFBXXj7Zi0xOr0FqBGID",
"date": "22/8/2019",
"message": "request message...",
"budget": "150",
"details":
" When would you like to have the cleaners over?":
"week",
"What type of cleaning package would you prefer?":
"answer for the type of clean",
"Do you need the cleaning tools and materials?":
"false",
"What time do you prefer to have the cleaners over?":
"8/20/2019"
我想在每次将数据发送到 Firestore 时将其添加为新的数组项
例如。
onTap()
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.setData("value": FieldValue.arrayUnion(values));
这里是代码
var values =
"tokens": "99",
"title": "Cleaning",
"location": " lat": "34.4333", "lng": "53.4343",
"id": "gVnGE5ZPRnQ8HYQq7dvT",
"client": "LFBXXj7Zi0xOr0FqBGID",
"date": "22/8/2019",
"message": "request message...",
"budget": "150",
"details":
" When would you like to have the cleaners over?":
"week",
"What type of cleaning package would you prefer?":
"answer for the type of clean",
"Do you need the cleaning tools and materials?":
"false",
"What time do you prefer to have the cleaners over?":
"8/20/2019"
onTap()
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.setData("value": FieldValue.arrayUnion(values));
【问题讨论】:
【参考方案1】:您正在使用覆盖现有值的setData
函数。您需要改用 updateData
函数,它将值添加到现有数组中。只需将您的 onTap
函数更改为:
onTap()
CollectionReference collection = this.collection(collection);
DocumentReference ref = collection.document(document);
ref.updateData("value": FieldValue.arrayUnion(values));
【讨论】:
以上是关于如何将地图存储到 Firestore 数组的主要内容,如果未能解决你的问题,请参考以下文章
无法访问 Firestore 快照中的数据以将标记添加到存储在 Firebase 中的 Google 地图