I have React application where I store data in firebase and use these data as state in my application.
I have two collections:
- one for storing Card data
-second for storing place and order in which the Cards will render (only Card's ID in array) second collection:
and both are used as state with mapStateToProps
.
However when I dispatch action to add new Card and update both collections at the same time (with batched writes) I don't get ID of document i just updated, only data.
This is function i dispatch to update both collections.
export const createTodo = (cardOrder,taskName) => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
var cardsColRef = firestore.collection("todos")
var orderDocRef = firestore.collection("cardOrder").doc(taskName)
var newCardRef = firestore.collection('todos').doc()
var batch = firestore.batch()
batch.set(newCardRef,{title : "",assignment:"",content: ""})
batch.update(orderDocRef,{order:[...cardOrder,newCardRef.id]})
batch.commit()}
Data before batched write.
cardOrder: Array(3)
0: {id: "2", order: Array(5)}
1: {id: "3", order: Array(2)}
2: {id: "order", order: Array(3)}
Data after batched write.
cardOrder: Array(3)
0: {order: Array(6)}
1: {id: "3", order: Array(2)}
2: {id: "order", order: Array(3)}
And application doesn't re-render itself, even though first collection has changed too.
After manual page refresh everything appears and works fine.
cardOrder: Array(3)
0: {id: "2", order: Array(6)}
1: {id: "3", order: Array(2)}
2: {id: "order", order: Array(3)}
Can anyone help with this ? I couldn't find anything similar to my question and I really need to know if there is solution to this