1

Objective

I am trying to pass some objects I get back from Firestore into my reducer so I can display some results back to the user.

Problem

When I try to call and pass the query to the reducer it does not appear to work. I am running a console.log to see if the reducer gets called but nothin it appearing. I think is because I have nested return statements?, Is this true?

Here is my action:

export const queryBidder = (value) => {
  return async (dispatch, getState, { getFirestore }) => {
    const firestore = getFirestore();
    const normalizeValue = _.capitalize(value);
    let Query = []
    firestore.collection("bidders").where("firstname", ">=", normalizeValue).get()
    .then(snapshot => {
      if (snapshot.empty) {
        console.log('No matching bidders.');
        return;
      }  
      snapshot.forEach(doc => {
        Query.push(doc.data());
        return { type: actionTypes.QUERYBIDDER, Query: Query };
      });
   
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });
  }
};

When I put the return statement above the async return statement, all works well. However, I need to be able to call getFirestore(). This setup comes from a tutorial I found on Udemy, to be honest I don't quite understand it.

return async (dispatch, getState, { getFirestore })

How are these getting passed in? Where do they come from? Why does the order matter? How can I just access the getfirestore() function without wrapping my actions logic in this function?

1
  • I was able to resolve this by using dispatch instead of return. Still would love to unerstand this setup a bit more.
    – Bromox
    Oct 12, 2019 at 19:37

2 Answers 2

0

I am unsure of why this worked but while awaiting for responses I opted to change from return to dispatch.

dispatch({ type: actionTypes.QUERYBIDDER, Query: Query });

this resolved my issue. Look for a more thorough answer of why this worked and my above original questions.

0

From redux documentation about using dispatch - This is the only way to trigger a state change. So basically if you want to change redux state you should dispatch an action with parameters - action type and new data.

You can read more about redux dispatch here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.