Ask coding questions

← Back to all posts
Fetch Request Body Not Logging
RossJames (51)


This repl has a webpage that makes a fetch request to a website, in the network tab of chrome, under response, i can see the message, but when logged in the console it just returns and nothing else. take a look plz, I am getting desperate. Thanks

Edit, Wrong link / repl

eankeen (1060)

so it doesnt look like a mistake with your code, it seems you caught an edge case

when you use fetch in your code, it returns a document with mime type text/html with the contents 'null'. not <head></head><body>null</body>, but null. from the sounds of it this seems intentional, that you are returning only null.

you can see in the screenshot, i comment out the res.text(), and print out body, the exact thing that fetch gives me: a Response Object. notice how the the value of body of the Response object is the reserved word null, not, the string "null".

when you uncomment use res.text(), from the looks of it, res.text() gets the body of your response object, and turns it into a string. since the body of your response object is null, and is not something res.text() is used to seeing, it tries to convert that null to a string. in this case, res.text() converts null to an empty string. you then log that empty string to the console, expecting to see null

usually, the body of a response object is a ReadableStream. calling res.text() in this case would do what you would expect, and convert that ReadableStream into a string

to fix this, you do not want to have just null in that html document returned by that website. if you are expecting null to be printed, you can put "null" in that html document, and it will print the string "null" to the console. if you want to print out the basic data type null, i would not immediately know how to do that.

you can use this repl i made to see how different values returned from the server can change what is printed out. one thing, if you decide to play around with the repl, note that typeof null will yield "object" (something you would not expect), and typeof "null" will yield "string" (what you would expect)

hopefully i was able to help :P

SixBeeps (1410)

The url ( returns null if you go to it in your browser, so it's a problem from analyticsmsg and not your code.

RossJames (51)

@niorg2606 it should log null, even when it isn't null, I still get the same problem

SixBeeps (1410)

@RossJames Ok, so what I've gathered (I have never used Express or Node before) is that res.text() returns a Promise object, which I'm guessing you're trying to store in the already-initialized res variable. It appears res does not want to get set, as when I deleted that line and replaced res with res.text(), I got a return value of "[object Promise]" as the result.

Take a look at this:

fetch('', {mode: 'no-cors'})
	.then(res => {
		console.log(res.text() + " is the response")
		console.log("Exception: ",res)