admin管理员组文章数量:1180551
Is there any way to update a specific index from the array in Firebase/firestore?
export const editComment = (comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
firestore.collection('topics').doc(comment.topicId).update({
comments: <--- this is array of objects
}).then(() => {
dispatch({ type: 'EDIT_COMMENT' })
}).catch((error) => {
dispatch({ type: 'EDIT_COMMENT_ERROR', error})
})
}
}
Is there any way to update a specific index from the array in Firebase/firestore?
export const editComment = (comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
firestore.collection('topics').doc(comment.topicId).update({
comments: <--- this is array of objects
}).then(() => {
dispatch({ type: 'EDIT_COMMENT' })
}).catch((error) => {
dispatch({ type: 'EDIT_COMMENT_ERROR', error})
})
}
}
Share
Improve this question
edited Jan 2, 2019 at 10:47
Fhranis Holdt
asked Jan 2, 2019 at 9:56
Fhranis HoldtFhranis Holdt
2011 gold badge3 silver badges10 bronze badges
6
- 1 Actually, you can't. You need to get data and then merge and update it. – Alexandr Tovmach Commented Jan 2, 2019 at 10:23
- Can you steer me how? i did something like this: stackoverflow.com/questions/53999186/… but it only works at the first update, when i trying second time i getting error – Fhranis Holdt Commented Jan 2, 2019 at 10:36
- You should create another collection inside the doc topicId. It is better than create an array or object inside a doc When the data getting bigger – Tony Bui Commented Jan 2, 2019 at 11:00
- all my components are based on this comments array so i need to rewritte whole code and add new collection instead of this array? why there is no solution for that simple and fundamentail update action, god... – Fhranis Holdt Commented Jan 2, 2019 at 11:14
- i created subsollection but when i getting data through mapStateToProps in react from firestore i only see normal data without created subcollection inside collection, any help? – Fhranis Holdt Commented Jan 2, 2019 at 12:53
3 Answers
Reset to default 27Is there any way to update a specific index from the array in Firestore?
No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.
Because we are creating apps that can be used in a multi user environment, try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time some other user might want to add another item at index 0. For sure, you'll end up having very different results and why not, get even array out of bounds exception. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index.
If those two methods do not help you enough, you should get the entire document, get the array, modify it and add it back to the database.
for me, I'm using firebase SDK for flutter, and nodeJS env. for cloud functions,, and I discovered the below solution.
this is a little bit of a hacky workaround
maybe we can save the list in a form of map not an array,, by using the array indexes as key strings to the sub maps
so you have structured the data like this in cloud fire store
key1: "value1" --> string
key2: --> map
> "0": {subKey1: "subValue1", subKey2: "subValue2"}
> "1": {subKey3: "subValue3", subKey4: "subValue4"}
> "2": {subKey5: "subValue5", subKey6: "subValue6"}
for example : if you want to update the subValue4, you may do this
await docReference.update({key2.1.subKey4: "theNewValueHere"});
and this ends up updating that document in cloud firestore to a document that looks like this
key1: "value1" --> string
key2: --> map
> "0": {subKey1: "subValue1", subKey2: "subValue2"}
> "1": {subKey3: "subValue3", subKey4: "theNewValueHere"}
> "2": {subKey5: "subValue5", subKey6: "subValue6"}
if you notice in firebase,, it updates the entire field key2
in this operation,, but gave you an easier access to update that index..
My problem is that I have to restructure all fields having list of maps to become maps within a map to have that access
And I will not do that,, will not refactor my entire db to do that,, I retrieve the entire doc,, (costs 1 read) modify at client and update the entire doc again (1 write)
I don't like it, but maybe someday I would use it
Turn the array into it's own separate document and update the field in there. That's what I did. I had to restructure my database a bit in order to do so.
本文标签: javascriptIs there any way to update a specific index from the array in FirestoreStack Overflow
版权声明:本文标题:javascript - Is there any way to update a specific index from the array in Firestore - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738126053a2065038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论