0

I am using react native to make a chat-like app. Postman is working properly while doing all the requests I made possible in .NET core API, but when I am trying to fetch from react native it gives the following error:

"null is not an object (evaluating 'blob.data')"

I have tried to look into this issue in different articles but haven't found anything.

return new Promise((resolve, reject) => {
            fetch('https://localhost:44305/api/replies', {
                method: 'POST',
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(qId)
            })
                .then(res => res.json())
                .then(result => resolve(result))
                .catch(err => reject(err))
        })

I am trying to get a list of answers to a question. Doing it from Postman works fine. I can't find a solution for this error.

I have tried adding return, just like somebody mentioned in a comment before fetch. The result is the same..

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ballo Adam
  • 105
  • 1
  • 8

2 Answers2

1

I thing your code must something like this:

return fetch(
   'https://localhost:44305/api/replies', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(qId)
    }
  )
  .then(res => res.json())
  .then(result => console.log(result))
  .catch(err => console.log(err));

You don't need to create a new promise 'cause fetch does it already.

Note console.log is a void. That means if you continue with thening the null exception will occure again. To solve that use this code at the second then and catch callback method.

console.log(result);
return result;
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • The code was already in quotes, when I copied it here something must have happened to it. In the edited response I added an image to see the exact error – Ballo Adam May 18 '19 at 17:39
  • @BalloAdam: I was still resolving the promise you've created. I've removed it. I'm working on mobile and debugging is a lot more difficult. – H. Pauwelyn May 18 '19 at 17:57
  • it looks like something is up with the fetch, in postman, I get back an array of objects. Funny thing is, in the previous projects I have used the same exact syntax with promises and it worked just fine. – Ballo Adam May 18 '19 at 18:00
  • @BalloAdam: can we continue in chat? https://chat.stackoverflow.com/rooms/193564/discussion-between-h-pauwelyn-and-ballo-adam – H. Pauwelyn May 18 '19 at 18:07
0

Solved, the problem was that on my Virtual Device I did not have access to localhost, so what I did, is that I changed the IP adress from my Debugger options in the API to the local ip adress

Ballo Adam
  • 105
  • 1
  • 8