react-native无法处理try catch块中的错误

时间:2019-02-11 20:25:27

标签: ios react-native

我正在尝试使用fetch()函数发送图像。显示红色屏幕和。发送数据之前出现“无此类文件或目录”错误。尝试catch块不处理此错误。

  

019-02-11 22:35:00.375   [错误] [tid:com.facebook.react.NetworkingQueue] [RCTNetworking.mm:272]   错误处理请求正文:Error Domain = NSCocoaErrorDomain   代码= 260“文件“ 5325CBBA-340E-4227-AE17-5D42021DF648.jpg”无法   打开,因为没有这样的文件。”   UserInfo = {NSFilePath = / Users / myuser / Library / Developer / CoreSimulator / Devices / 9362AC50-7C82-4C8D-B686-39A6718CBA5F / data / Containers / Data / Application / 64CD29E3-2BD9-4D9A-B168-4B12A848889C / Documents / images / 5325CBBA-340E-4227-AE17-5D42021DF648.jpg,   NSUnderlyingError = 0x600003c0a790 {Error Domain = NSPOSIXErrorDomain   代码= 2“没有这样的文件或目录”}}

如何处理此错误?

错误屏幕:https://i.hizliresim.com/bVvqrm.png

  VeriListele() {

    try {
      db.transaction((tx) => {
        tx.executeSql("select * from " + TABLO_DATA + " where " + ROW_GONDERILDI_ + "=0", [], (tx, results) => {
          console.log("Query completed2");

          // Get rows with Web SQL Database spec compliance.
          console.log(results.rows);
          var len = results.rows.length;
          const data = new FormData();
          for (let i = 0; i < len; i++) {
            try {
              let row = results.rows.item(i);
              console.log(row);
              var FotoString='';
              if (row.resim1!='' && row.resim1!='undefined') {
                var resimjson=JSON.parse(row.resim1);
                FotoString+='&photo[]='+row.resim1;
                data.append('photo1[]', {
                  uri: resimjson.uri,
                  type: resimjson.type, // or photo.type
                  name: 'photo'
                });
              }
              if (row.resim2!='' && row.resim2!='undefined') {
                FotoString+='&photo[]='+row.resim2;
                var resimjson=JSON.parse(row.resim2);
                data.append('photo1[]', {
                  uri: resimjson.uri,
                  type:resimjson.type, // or photo.type
                  name: 'photo'
                });
              }
              if (row.resim3!='' && row.resim3!='undefined') {
                FotoString+='&photo[]='+row.resim3;
                var resimjson=JSON.parse(row.resim3);
                data.append('photo1[]', {
                  uri: resimjson.uri,
                  type: resimjson.type, // or photo.type
                  name: 'photo'
                });
              }
              if (row.resim4!='' && row.resim4!='undefined') {
                FotoString+='&photo[]='+row.resim4;
                var resimjson=JSON.parse(row.resim4);
                data.append('photo1[]', {
                  uri:resimjson.uri,
                  type: resimjson.type, // or photo.type
                  name: 'photo'
                });
              }
              if (row.video!='' && row.video!='undefined') {
                FotoString+='&video[]='+row.video;
                var resimjson=JSON.parse(row.video);
                data.append('video1[]', {
                  uri: resimjson.uri,
                  type: resimjson.type, // or photo.type
                  name: 'video'
                });
              }
              try {

              } catch (error) {

              }


          data.append('api_token', Global.GToken); // you can append anyone.
          data.append('resid', row.id); // you can append anyone.

          data.append('additional', row.ek_aciklama); // you can append anyone.

          data.append('issue_date', row.tarih.split(' ')[0]); // you can append anyone.
          try {
            fetch("https://myapi.com/add", {
              method: 'post',
              body: data
            }).then(function(response){
              console.log(response);
             var responseJson=JSON.parse(response._bodyText)
            //  console.log();

              console.log(response);
            //  console.log(responseJson);
            //  console.log(responseJson);
              if (responseJson.sonuc == 1) {
                db.transaction((tx1) => {
                  console.log(row.id);
                  tx1.executeSql("update " + TABLO_DATA + " set gonderildi=1 where id=" + row.id + "");
                });


              } else if (responseJson.sonuc==0) {
                console.log('basarisiz');
                db.transaction((tx1) => {
                 // tx1.executeSql("update " + TABLO_DATA + " set gonderildi=1 where id=" + row.id + "");
                });
              }
            }).catch(error =>console.log(error) )
          } catch (error) {
            console.log('bb');
          }




            } catch (error) {
              console.log('ss');
            }

          }
        });
      });

    } catch (error) {
      console.log('dd');
    }
    //  const Veriler = await AsyncStorage.getItem(‘products’)
    }

1 个答案:

答案 0 :(得分:0)

我看到有关丢失文件的错误日志,但是看不到与fetch调用的关系。您要发布该文件吗?如果是这样,您可以在代码中加以澄清吗?

如果您未在fetch调用中使用该文件,则需要弄清楚该文件的使用位置以及为什么不使用它。

仅供参考,您在.catch之后有一个fetch,该日志记录错误,然后防止任何fetch错误陷入try/catch中。