0

hello guys i am working with a react native project i am working with offline so i have to store data offline and when the internet is back i must redirect the user to a screen and send data to web and change a percentage but my problem is that the fetch render twice and the data returned after all promis is undefined and the fetch to server passed by the way in server and data base is changer well

here is my functions

export const syncOfflinedata = activities => {
  return dispatch => {
    Promise.all(
      activities.map(item => {
        Api()
          .put(putEvaluation(item._id), {evaluation: item.evaluation})
          .then(data => {
            dispatch(setProgressporcentage(1 / activities.length));
          })
          .catch(err => {
            console.log(err.message);
          });
      }),
    )
      .then(datafinished => {
        console.log('finished', datafinished);
      })
      .catch(e => {
        console.log(e);
      });
  };
};
  • 1
    You don't return anything from your map callback, so you're going to have an array full of undefineds. You all so don't return anything from your .then handler that calls dispatch, so even if you return the Promise it will resolve to undefined. Add some returns. – Jared Smith Mar 17 '20 at 11:38

1 Answers1

0

The map method should have return unless it is written in its short form in just one line:

export const syncOfflinedata = activities => {
  return dispatch => {
    Promise.all(
      activities.map(item => {
        return Api()
          .put(putEvaluation(item._id), {evaluation: item.evaluation})
          .then(data => {
            dispatch(setProgressporcentage(1 / activities.length));
          })
          .catch(err => {
            console.log(err.message);
          });
      }),
    )
    .then(datafinished => {
      console.log('finished', datafinished);
    })
    .catch(e => {
      console.log(e);
    });
  };
};
Buddy Christ
  • 1,364
  • 8
  • 22
  • do u have any idea why every fetch render twice ? –  Mar 17 '20 at 11:44
  • not from that piece of code, place a `console.log` and check the `activities` content before using `Promise.all`. Check they are not repeated – Buddy Christ Mar 17 '20 at 11:47
  • i have only two items in activities and still get undefined my friend –  Mar 17 '20 at 11:54