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

9
  • is your question about typescript or javascript? That' unclear to me. Commented May 18, 2019 at 16:01
  • 1
    About typescript, since the fetch is not working. Commented May 18, 2019 at 16:02
  • When I try to use fetch, i get the error mentioned above, yet in postman it works just fine Commented May 18, 2019 at 16:03
  • Maby this can help you stackoverflow.com/questions/41103360/… Commented May 18, 2019 at 16:05
  • 2
    Possible duplicate of How to use fetch in typescript Commented May 18, 2019 at 17:49

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

4 Comments

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
@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.
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.
@BalloAdam: can we continue in chat? chat.stackoverflow.com/rooms/193564/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.