Redux-saga not able to read react-native

2019-08-26 12:36发布

问题:

I am trying a read a Image File using react-native-fs in react-native.

Below is my generator function in saga.js:

function* uploadCardLogo(logo, userId) {

  console.log("uploading card logo"); //being logged
  const storageRef = firebase.storage().ref('${userId}').child("mycards/logos/");
  console.log("storage ref fetched " + logo.uri); //being logged

  const cardLogo = yield call(RNFS.readFile, logo.uri); // Not throwing any errors
  console.log("reading file done") // Not being logged

  const logoStorageUrl = yield call([storageRef, storageRef.put], cardLogo);
  console.log("Logo has been update = ${logoStorageUrl}");
  return logoStorageUrl;
}

As you can see, I am not able to read the Image file nor am I getting any errors, Can anyone please tell me what I am doing wrong?

EDIT:

Below is my function from which uploadCardLogo is being called:

function* uploadCard(action) {
  console.log("about to upload card");
  console.log(action.card);

  try{
      const userId = firebase.auth().currentUser.uid;
      const database = firebase.database();
      const logoStorageUrl = yield call(uploadCardLogo, action.card.logo, userId);
      const ref = database.ref('users/'+userId+'/myCards');
      const result = yield call([ref, ref.set], action.card);
      console.log("Card Added To Realtime Database");
      yield put({type: CARD_UPLOADED, card: action.card});
      console.log("about to update app state");
  }
  catch(error) {
    const error_message = {code: error.code, message: error.message};
    yield put({type: CARD_UPLOAD_ERROR, error: error_message})
  }
}

回答1:

I'm not sure what RNFS.readFile is but in order to catch the error you have to wrap your call into a try-catch block:

try {
  const cardLogo = yield call(RNFS.readFile, logo.uri);
} catch (error) {
  console.log(error);
}

If you don't do this here the error will be propagated to the parent saga and if there is a try-catch block it will catch it.