Search code examples
javascriptfirebasegoogle-cloud-firestorereduxreact-redux-firebase

How do you add data to firebase doc using redux thunk getFireBase and getFirestore from react-redux-firebase


Problem

  • I'm having trouble adding data to the firebase database using react-redux-thunk
  • I've checked the documentation and watched multiple youtube videos but keeping running into error after error
  • I want to add data to a firebase doc using the getFirestore and getFirebase middleware from the "redux-firestore" and "react-redux-firebase" library respectively

Code

  • Here is the action function
  • All the code seems to be working but I can't figure out how to add validatonResults to users/uid (see comment in code below)
  • Tried the following code (and a lot of other stuff) but it didn't work firebase.push('users', { data: validatonResults })
import inputValidationAPI from "../../apis/inputValidationAPI"

export const validateTestCase = payload => {
    return (dispatch, getState, { getFirebase, getFirestore }) => {
        const fireStore = getFirestore()
        const firebase = getFirebase()
        const uid = getState().firebase.auth.uid

        inputValidationAPI
            .post("/submitTerm", {
                term: payload
            })
            .then(response => {
                return response.data.data
            })
            .then(validatonResults => {
                /* 
                validationResults = ["string1", "string2", "string3"]
                I want to take validationResults and add them into firebase collection something like:
                
                firebase.push('todos/${uid}', { some: 'data' }) 
                */

                return validatonResults
            })
            .then(validatonResults => {
                dispatch({
                    type: "VALIDATE_TESTCASE_SUCCESS",
                    payload: validatonResults
                })
            })
            .catch(err => console.log(err))
    }
}

Solution

  • The following syntax solved this:

    fireStore.update(
              { collection: "users", doc: uid },
              { dataToSave: someJSONObject },
         }
    )