Try to search your question here, if you can't find : Ask Any Question Now ?

Why this array is coming empty?

HomeCategory: stackoverflowWhy this array is coming empty?
kundan asked 3 weeks ago

Im doing a fetch on some data at firebase at my firebase app.

I triple check, and debugged the code. The first fetch returns me 5 items on the array. Then I need to get the id of the user and do another fetch to get the data from the user. This is my code:

export const fetchStyleItems = () => async dispatch => {
  dispatch({ type: GET_STYLE_ITEMS_REQUEST });
  database
    .ref("styles_items")
    .orderByChild("posted")
    .once("value")
    .then(function(snapshot) {
      const exists = snapshot.val() !== null;
      if (exists) data = snapshot.val();
      var photo_feed = [];
      for (var photo in data) {
        const photoObj = data[photo];
        database
          .ref("users")
          .child(photoObj.author)
          .once("value")
          .then(function(snapshot) {
            const exists = snapshot.val() !== null;
            if (exists) data = snapshot.val();
            console.log(`inside the lasso ${photoObj} `);
            photo_feed.push({
               /...

            });
          })
          .catch(error => console.log(error));
      }
      console.log();
      dispatch({
        type: GET_STYLE_ITEMS_SUCCESS,
        payload: photo_feed
      });
    })
    .catch(error => console.log(error));
};

The problem. When I debug my code, and try to dispatch the GET_STYLE_ITEMS_SUCCESS the array photo_feed is coming empty.

I think Im missing some scope here. But Im stuck for 3 hours right now and cant see what is wrong.

1 Answers
Best Answer
Mannu answered 3 weeks ago
Your Answer

15 + 10 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com