Redux-saga 无法读取 react-native [英] Redux-saga not able to read react-native

查看:41
本文介绍了Redux-saga 无法读取 react-native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 react-native 中的 react-native-fs 读取 Image File.

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

下面是我在 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?

下面是调用 uploadCardLogo 的函数:

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})
  }
}

推荐答案

我不确定 RNFS.readFile 是什么,但为了捕获错误,您必须封装 调用 进入 try-catch 块:

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);
}

如果您不这样做,错误将传播到父 saga,如果有 try-catch 块,它将捕获它.

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.

这篇关于Redux-saga 无法读取 react-native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆