0

I am running a NodeJS fetch() in my React Native app to get data from my Flask Heroku server

This is what the Flask server does:

(I'm sure it returns this normally because when I go to the URL in my browser, it is a blank page with the word "false")

@app.route('/login')
def login():
    
    #some code that I'm sure is working and doesn't need to be shown
    
    return jsonify(False)

This is the code that the React Native app does:

console.log(address)

var returned = await fetch(address)

console.log(JSON.stringify(returned))

I know the address that it is using, since I'm printing it to the console. I have gone to this exact URL, copy-pasted from my console, in the browser and the data returned looks fine

Here is what the React Native fetch() returns:

{"type":"default","status":200,"ok":true,"headers":{"map":{"content-length":"6","connection":"keep-alive","content-type":"application/json","date":"Wed, 26 Aug 2020 17:46:31 GMT","via":"1.1 vegur","server":"gunicorn/20.0.4","cache-control":"public, max-age=0"}},"url":"[MY_URL_IS_HERE]/login?username=W&tag=L","bodyUsed":false,"_bodyInit":{"_data":{"size":6,"offset":0,"blobId":"e450e243-9a7e-4a35-8e47-f8e9f3c7a58c","__collector":{}}},"_bodyBlob":{"_data":{"size":6,"offset":0,"blobId":"e450e243-9a7e-4a35-8e47-f8e9f3c7a58c","__collector":{}}}}

I've looked at many guides and StackOverflow posts online, but none of them have helped me parse/get better data than this.

How do I get my data?

8
  • They're just headers aren't they? Commented Aug 26, 2020 at 18:05
  • I think so, but I don't know where to find my data in these headers, or if it's even there. Most tutorials have made it seem as simple as a "JSON.stringify(returned)", "returned.blob()", or something similar, though none of them have seemed to work for me. Commented Aug 26, 2020 at 18:07
  • Ok, well I don't understand the downvote, but you can paste into jsonlint.com and see the response. Some of that response does seem unnecessary but I don't think I'm seeing the full picture Commented Aug 26, 2020 at 18:08
  • Yeah, look at what Ayush posted, you are logging the fetch request, not the data, you need to pull out just the data from the fetch request. I would recommend you look at the fetch api. developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch Commented Aug 26, 2020 at 18:09
  • @WarrenJennings this is basically what fetch returns: developer.mozilla.org/en-US/docs/Web/API/Response so to read the response body you need to use the functions .json, .text etc, as mentioned in this link Commented Aug 26, 2020 at 18:10

1 Answer 1

0

@AyushGupta pointed out that I needed to change:

var returned = await fetch(address)

to:

var returned = (await (await fetch(address)).json())
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.