I am basically working on an app where people can ask questions. So when the user posts a new question, I update my redux store. And then I push the changes to firebase. But the thing is that the new changes are not reflected in the firebase as the redux update takes some time and by that time the firebase update function has already been called. How do I change this?
Code where I update my store and call the firebase update.
this.props.newQuestion(this.state.addedData);
console.log("Hello there " + JSON.stringify(this.props.user));
let temp = this.props.user;
console.log("This is temp" + JSON.stringify(temp));
firestore()
.collection("users")
.doc(this.props.user.id)
.update({
user: temp,
})
.then(() => {
console.log("From the then" + JSON.stringify(this.props.user));
console.log("User updated!");
})
.catch((err) => {
console.log(err);
});
Definition of my dispatcher for this redux update function -
case ADD_QUESTION:
//console.log("From the actions" + JSON.stringify(action.question));
return {
...state,
user: {
id: state.user.id,
name: state.user.name,
email: state.user.email,
courses: state.user.courses,
// adding the new question everytime
questions: state.user.questions.concat(action.question),
},
};