Update value in object in nested array in React Firestore
I need to update value field in object at specific index which is in array in Firebase
I tried to grab through getState()
array with all objects;
then get object index which I need;
then assign new value to object, in this case content;
then rewrite whole array (comments
) to newArray (actualComments
) as you can see below.
And this works how I want, but only for the first time. If I try to do it again, I get error TypeError: "content" is read-only
.
export const editComment = (comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
actualComments[numberArray].content = comment.editContent;
firestore.collection('topics').doc(comment.topicId).update({
comments: actualComments
}).then(() => {
dispatch({ type: 'EDIT_COMMENT' })
}).catch((error) => {
dispatch({ type: 'EDIT_COMMENT_ERROR', error})
})
}
}
javascript reactjs firebase redux google-cloud-firestore
add a comment |
I need to update value field in object at specific index which is in array in Firebase
I tried to grab through getState()
array with all objects;
then get object index which I need;
then assign new value to object, in this case content;
then rewrite whole array (comments
) to newArray (actualComments
) as you can see below.
And this works how I want, but only for the first time. If I try to do it again, I get error TypeError: "content" is read-only
.
export const editComment = (comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
actualComments[numberArray].content = comment.editContent;
firestore.collection('topics').doc(comment.topicId).update({
comments: actualComments
}).then(() => {
dispatch({ type: 'EDIT_COMMENT' })
}).catch((error) => {
dispatch({ type: 'EDIT_COMMENT_ERROR', error})
})
}
}
javascript reactjs firebase redux google-cloud-firestore
1
If your are using redux as your state manager, your state is supposed to be immutable, it appears weird to me that it doesn't complain on the first time. Have you tried updating your data through a reducer ?
– Kevin Coulibaly
Jan 2 at 0:09
i trying update value of one object inside firestorm database in async action (thunk) before reducer. Each 'comment' data which i render dynamicly to DOM from database is inside object in array,
– Fhranis Holdt
Jan 2 at 9:38
add a comment |
I need to update value field in object at specific index which is in array in Firebase
I tried to grab through getState()
array with all objects;
then get object index which I need;
then assign new value to object, in this case content;
then rewrite whole array (comments
) to newArray (actualComments
) as you can see below.
And this works how I want, but only for the first time. If I try to do it again, I get error TypeError: "content" is read-only
.
export const editComment = (comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
actualComments[numberArray].content = comment.editContent;
firestore.collection('topics').doc(comment.topicId).update({
comments: actualComments
}).then(() => {
dispatch({ type: 'EDIT_COMMENT' })
}).catch((error) => {
dispatch({ type: 'EDIT_COMMENT_ERROR', error})
})
}
}
javascript reactjs firebase redux google-cloud-firestore
I need to update value field in object at specific index which is in array in Firebase
I tried to grab through getState()
array with all objects;
then get object index which I need;
then assign new value to object, in this case content;
then rewrite whole array (comments
) to newArray (actualComments
) as you can see below.
And this works how I want, but only for the first time. If I try to do it again, I get error TypeError: "content" is read-only
.
export const editComment = (comment) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
actualComments[numberArray].content = comment.editContent;
firestore.collection('topics').doc(comment.topicId).update({
comments: actualComments
}).then(() => {
dispatch({ type: 'EDIT_COMMENT' })
}).catch((error) => {
dispatch({ type: 'EDIT_COMMENT_ERROR', error})
})
}
}
javascript reactjs firebase redux google-cloud-firestore
javascript reactjs firebase redux google-cloud-firestore
edited Jan 2 at 11:10
wanttobeprofessional
1,03131323
1,03131323
asked Jan 1 at 21:44
Fhranis HoldtFhranis Holdt
185
185
1
If your are using redux as your state manager, your state is supposed to be immutable, it appears weird to me that it doesn't complain on the first time. Have you tried updating your data through a reducer ?
– Kevin Coulibaly
Jan 2 at 0:09
i trying update value of one object inside firestorm database in async action (thunk) before reducer. Each 'comment' data which i render dynamicly to DOM from database is inside object in array,
– Fhranis Holdt
Jan 2 at 9:38
add a comment |
1
If your are using redux as your state manager, your state is supposed to be immutable, it appears weird to me that it doesn't complain on the first time. Have you tried updating your data through a reducer ?
– Kevin Coulibaly
Jan 2 at 0:09
i trying update value of one object inside firestorm database in async action (thunk) before reducer. Each 'comment' data which i render dynamicly to DOM from database is inside object in array,
– Fhranis Holdt
Jan 2 at 9:38
1
1
If your are using redux as your state manager, your state is supposed to be immutable, it appears weird to me that it doesn't complain on the first time. Have you tried updating your data through a reducer ?
– Kevin Coulibaly
Jan 2 at 0:09
If your are using redux as your state manager, your state is supposed to be immutable, it appears weird to me that it doesn't complain on the first time. Have you tried updating your data through a reducer ?
– Kevin Coulibaly
Jan 2 at 0:09
i trying update value of one object inside firestorm database in async action (thunk) before reducer. Each 'comment' data which i render dynamicly to DOM from database is inside object in array,
– Fhranis Holdt
Jan 2 at 9:38
i trying update value of one object inside firestorm database in async action (thunk) before reducer. Each 'comment' data which i render dynamicly to DOM from database is inside object in array,
– Fhranis Holdt
Jan 2 at 9:38
add a comment |
1 Answer
1
active
oldest
votes
My friend helped me with this, and now my updates in object at specifix index in Array works! Here is code, cheers
/////Grab through reference all comments Array in firestore
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
////make container for array
let updatedComments = ;
//// copy array from reference to empty updatedComments array
actualComments.forEach(comment => {
updatedComments.push({
content: comment.content,
createdAt: comment.createdAt,
editDate: comment.editDate,
edited: comment.edited,
id: comment.id,
idTopic: comment.idTopic,
name: comment.name,
nameId: comment.nameId
})
})
//// grab index which i want to update
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
//// update in specific index array
updatedComments[numberArray].content = comment.editContent;
updatedComments[numberArray].editDate = new Date();
updatedComments[numberArray].edited = true;
//// replace updated array in firestore
firestore.collection('topics').doc(comment.topicId).update({
comments: updatedComments
}).then...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999186%2fupdate-value-in-object-in-nested-array-in-react-firestore%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
My friend helped me with this, and now my updates in object at specifix index in Array works! Here is code, cheers
/////Grab through reference all comments Array in firestore
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
////make container for array
let updatedComments = ;
//// copy array from reference to empty updatedComments array
actualComments.forEach(comment => {
updatedComments.push({
content: comment.content,
createdAt: comment.createdAt,
editDate: comment.editDate,
edited: comment.edited,
id: comment.id,
idTopic: comment.idTopic,
name: comment.name,
nameId: comment.nameId
})
})
//// grab index which i want to update
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
//// update in specific index array
updatedComments[numberArray].content = comment.editContent;
updatedComments[numberArray].editDate = new Date();
updatedComments[numberArray].edited = true;
//// replace updated array in firestore
firestore.collection('topics').doc(comment.topicId).update({
comments: updatedComments
}).then...
add a comment |
My friend helped me with this, and now my updates in object at specifix index in Array works! Here is code, cheers
/////Grab through reference all comments Array in firestore
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
////make container for array
let updatedComments = ;
//// copy array from reference to empty updatedComments array
actualComments.forEach(comment => {
updatedComments.push({
content: comment.content,
createdAt: comment.createdAt,
editDate: comment.editDate,
edited: comment.edited,
id: comment.id,
idTopic: comment.idTopic,
name: comment.name,
nameId: comment.nameId
})
})
//// grab index which i want to update
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
//// update in specific index array
updatedComments[numberArray].content = comment.editContent;
updatedComments[numberArray].editDate = new Date();
updatedComments[numberArray].edited = true;
//// replace updated array in firestore
firestore.collection('topics').doc(comment.topicId).update({
comments: updatedComments
}).then...
add a comment |
My friend helped me with this, and now my updates in object at specifix index in Array works! Here is code, cheers
/////Grab through reference all comments Array in firestore
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
////make container for array
let updatedComments = ;
//// copy array from reference to empty updatedComments array
actualComments.forEach(comment => {
updatedComments.push({
content: comment.content,
createdAt: comment.createdAt,
editDate: comment.editDate,
edited: comment.edited,
id: comment.id,
idTopic: comment.idTopic,
name: comment.name,
nameId: comment.nameId
})
})
//// grab index which i want to update
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
//// update in specific index array
updatedComments[numberArray].content = comment.editContent;
updatedComments[numberArray].editDate = new Date();
updatedComments[numberArray].edited = true;
//// replace updated array in firestore
firestore.collection('topics').doc(comment.topicId).update({
comments: updatedComments
}).then...
My friend helped me with this, and now my updates in object at specifix index in Array works! Here is code, cheers
/////Grab through reference all comments Array in firestore
let actualComments = getState().firestore.data.topics[comment.topicId].comments;
////make container for array
let updatedComments = ;
//// copy array from reference to empty updatedComments array
actualComments.forEach(comment => {
updatedComments.push({
content: comment.content,
createdAt: comment.createdAt,
editDate: comment.editDate,
edited: comment.edited,
id: comment.id,
idTopic: comment.idTopic,
name: comment.name,
nameId: comment.nameId
})
})
//// grab index which i want to update
let numberArray = actualComments.findIndex(e => {return e.id === comment.commentId});
//// update in specific index array
updatedComments[numberArray].content = comment.editContent;
updatedComments[numberArray].editDate = new Date();
updatedComments[numberArray].edited = true;
//// replace updated array in firestore
firestore.collection('topics').doc(comment.topicId).update({
comments: updatedComments
}).then...
answered Jan 4 at 13:25
Fhranis HoldtFhranis Holdt
185
185
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999186%2fupdate-value-in-object-in-nested-array-in-react-firestore%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If your are using redux as your state manager, your state is supposed to be immutable, it appears weird to me that it doesn't complain on the first time. Have you tried updating your data through a reducer ?
– Kevin Coulibaly
Jan 2 at 0:09
i trying update value of one object inside firestorm database in async action (thunk) before reducer. Each 'comment' data which i render dynamicly to DOM from database is inside object in array,
– Fhranis Holdt
Jan 2 at 9:38