[英]Firestore Update single item in an array field
I have a document in Firebase Firestore that is something like the below.我在 Firebase Firestore 中有一份文档,内容如下所示。 The main point here is that I have an array called items
with objects inside it:这里的要点是我有一个名为items
的数组,其中包含对象:
{
name: 'Foo',
items: [
{
name: 'Bar',
meta: {
image: 'xyz.png',
description: 'hello world'
}
},
{
name: 'Rawr',
meta: {
image: 'abc.png',
description: 'hello tom'
}
}
]
}
I am trying to update a field inside the item array, under the meta object. For example items[0].meta.description from hello world to hello bar我正在尝试更新项目数组中的字段,在元 object 下。例如 items[0].meta.description 从 hello world 到 hello bar
Initially I attempted to do this:最初我试图这样做:
const key = `items.${this.state.index}.meta.description`
const property = `hello bar`;
this.design.update({
[key]: property
})
.then(() => {
console.log("done")
})
.catch(function(error) {
message.error(error.message);
});
This didn't appear to work though, as it removed everything in the item index I wanted to modify, and just kept the description under the meta object但这似乎没有用,因为它删除了我想修改的项目索引中的所有内容,并且只是将描述保留在元 object 下
I am now trying the following which basically rewrites the whole meta object with the new data我现在正在尝试以下基本上用新数据重写整个元 object
const key = `items.${this.state.index}.meta`
const property = e.target.value;
let meta = this.state.meta;
meta[e.target.id] = property;
this.design.update({
[key]: meta
})
.then(() => {
this.setState({
[key]: meta
})
})
.catch(function(error) {
message.error(error.message);
});
Unfortunately though, this seems to turn my whole items array into an object that looks something like:不幸的是,这似乎将我的整个项目数组变成了一个 object,看起来像这样:
{
name: 'Foo',
items: {
0: {
name: 'Bar',
meta: {
image: 'xyz.png',
description: 'hello world'
}
},
1: {
name: 'Rawr',
meta: {
image: 'abc.png',
description: 'hello tom'
}
}
}
}
Any ideas how I can just update the content I want to?有什么想法可以更新我想要的内容吗?
Firestore doesn't have the ability to update an existing element in an indexed array. Firestore 无法更新索引数组中的现有元素。 Your only array options for updates are described in the documentation - you can add a new element to the array ("arrayUnion") or remove an element ("arrayRemove"). 文档中描述了您唯一的更新数组选项 - 您可以向数组添加新元素(“arrayUnion”)或删除元素(“arrayRemove”)。
As an alternative, you can read the entire array out of the document, make modifications to it in memory, then update the modified array field entirely.作为替代方案,您可以从文档中读取整个数组,在内存中对其进行修改,然后完全更新修改后的数组字段。
You can make a separate collection for that particular array, like this in this picture earlier I had different fields (no collections) of name, email and pages, And in this, I wanted to change the data of a specific page that is inside the array.您可以为该特定数组创建一个单独的集合,就像之前这张图片中的这个一样,我有不同的名称字段(没有集合),email 和页面,在此,我想更改特定页面内的数据大批。 For that, I made a different collection of pages with individual documents of each page having values of title description and content which can be mutated.为此,我制作了一个不同的页面集合,每个页面的单独文档都具有标题描述和可以改变的内容的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.