Fetch API OutPut issues = ƒ json() { [native code] }

// Get detail for a random user
const root = 'https://jsonplaceholder.typicode.com';
let id = Math.floor(Math.random() * 20) + 1;
let uri = root + '/users/' + id;
console.log('FETCH:', 'uri');
fetch(uri)
			.then(function( response ){
				return response.json;
			})
			.then( (data) =>{
				console.log(data);
				let jsonData = JSON.stringify(data);
        console.log(data);

				let output = document.getElementById('output');
        output.textContent = jsonData;
				console.log(jsonData);
			})
			.catch((err) =>{
				console.log('ERROR', err.message);
			});

Live

Question:
Where am I faltering? why the output is not generated when response code is valid.

I got my mistake after posting it:

response.json; should be:

response.json();

3 Likes

Hi @codeispoetry, just as an aside – if you’re only stringifying the response anyway you might just use response.text() here:

const root = 'https://jsonplaceholder.typicode.com'
const id = Math.floor(Math.random() * 20) + 1
const uri = root + '/users/' + id

fetch(uri).then(function (response) {
  return response.text()
}).then(function (data) {
  document.body.textContent = data
}).catch(console.error)
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.