React-native-fetch-blob: Error calling RCTDeviceEventEmitter.emit failed to create value from json

Created on 25 Jul 2018  路  3Comments  路  Source: wkh237/react-native-fetch-blob

device-2018-07-25-104424

Here is my file downloading code as below -

async downloadFile(fileURL) {
    const timeout = new Promise((resolve, reject) => {
      setTimeout(reject, 5000, "Request timed out");
    });

    **_const request = fetch(fileURL);_**

    return Promise.race([timeout, request])
      .then(response => {
        // alert(JSON.stringify(response, null, 4));
        if (response.status !== 404) {
          var filename = fileURL.substring(fileURL.lastIndexOf("/") + 1);

          const { config, fs } = RNFetchBlob;
          let DownloadDir = fs.dirs.DownloadDir;
          let options = {
            fileCache: true,
            addAndroidDownloads: {
              useDownloadManager: true, // setting it to true will use the device's native download manager and will be shown in the notification bar.
              notification: false,
              path: DownloadDir + "/MyApp/" + filename, // this is the path where your downloaded file will live in
              description: "Downloading Payslip."
            }
          };
          RNFetchBlob.config(options)
            .fetch("GET", fileURL, {})
            .then(res => {
              // the path should be dirs.DocumentDir + 'path-to-file.anything'
              alert("The file saved to " + res.path());
            })
            .catch((errorMessage, statusCode) => {
              // This is never hit for a server 404 error
              console.log(errorMessage, statusCode);
              Alert.alert(
                "MyApp",
                "Payslip not available.",
                [{ text: "OK" }],
                { cancelable: true }
              );
            });
        } else {
          Alert.alert(
            "MyApp",
            "Payslip not available.",
            [{ text: "OK" }],
            { cancelable: true }
          );
        }
      })
      .catch(error => {
        Alert.alert(
          "MyApp",
          "Payslip not available.",
          [{ text: "OK" }],
          {
            cancelable: true
          }
        );
      });

    return;
  }

I am getting the above error on the highlighted line , i.e, as below

const request = fetch(fileURL);

The error, I am getting in log is as follow -

07-25 10:44:15.128 9695-9732/com.MyApp E/unknown:ReactNative: Exception in native call
    java.lang.RuntimeException: Error calling RCTDeviceEventEmitter.emit
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
        at android.os.Looper.loop(Looper.java:164)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: com.facebook.jni.CppException: Failed to create Value from JSON: 
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)聽
        at android.os.Handler.handleCallback(Handler.java:789)聽
        at android.os.Handler.dispatchMessage(Handler.java:98)聽
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)聽
        at android.os.Looper.loop(Looper.java:164)聽
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192)聽
        at java.lang.Thread.run(Thread.java:764)聽

I double-checked the url and it seems fine, and file is getting downloaded as well.

Most helpful comment

Hello,

What is your problem? and how can you fix it?

Can you share information @NarendraSingh88 :)

All 3 comments

Hello,

What is your problem? and how can you fix it?

Can you share information @NarendraSingh88 :)

i get this error only when i disable debug mode. There is no error on debug mode on android emulator . So i cannot find where is the error. somebody can help?

@NarendraSingh88 how did you fixed it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicholasstephan picture nicholasstephan  路  3Comments

Sam1301 picture Sam1301  路  3Comments

aouaki picture aouaki  路  3Comments

mykelaballe picture mykelaballe  路  4Comments

Jason0112 picture Jason0112  路  3Comments