Redux-saga无法读取本地反应

时间:2018-01-15 13:25:09

标签: javascript react-native react-redux redux-saga

我正在尝试使用react-native中的react-native-fs读取图像文件

以下是 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;
}

正如您所看到的,我无法阅读图像文件,也没有收到任何错误,有人可以告诉我我做错了什么吗?

修改

以下是调用uploadCardLogo的函数:

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 个答案:

答案 0 :(得分:1)

我不确定RNFS.readFile是什么,但为了捕获错误,您需要将call包装到try-catch块中:

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

如果您不在此处执行此操作,则错误将传播到父级传奇,如果存在try-catch块,则会捕获该错误。