2

I have tried the following code:

const response =await fetch('https://facebook.github.io/react-native/movies.json');
const json= await response.json();
console.log(json.result);

to print fetched JSON data to the console but it's not working. How can I write fetched data directly to console?

2
  • 1
    await is used with a promise. You can get to log after you resolve the promise. Commented Jan 23, 2018 at 19:24
  • There is an issue with your code. I’ve demonstrated a solution below. However asking about using the data to create a view should be a separate question, as it’s unrelated to your code sample. Commented Jan 23, 2018 at 20:50

5 Answers 5

7

An answer using sync/await (which you were using in your question)

const fetchAndLog = async () => {
    const response = await fetch('https://facebook.github.io/react-native/movies.json');
    const json = await response.json();
    // just log ‘json’
    console.log(json);
}

fetchAndLog();
Sign up to request clarification or add additional context in comments.

1 Comment

I will not print at all if the response is not a valid JSON (eg server failure)
6
fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
    console.log(responseJson);
})

Comments

2

I am using this way it is working fine.

fetch('https://facebook.github.io/react-native/movies.json')  
 .then((response) => response.text())
 .then((responseText) => {
     console.log(JSON.parse(responseText));
 })
 .catch((error) => {
     console.log("reset client error-------",error);
});

Below is used for particular method request. Headers and Body are used for send data to server. For this way we can request type and method for fetch function.

      fetch(url, {
            method: 'POST', 
            timeout:10000,
            headers: headers,
            body: JSON.stringify(params) 
        })  
        .then((response) => response.text())
        .then((responseText) => {
             console.log(JSON.parse(responseText));
        })
        .catch((error) => {
             console.log("reset client error-------",error);
        });
    });

1 Comment

It is working fine. Thank you for giving detail about requestType or Method used in fetch operation.
1

In my opinion, this is the right way to write :

async function fetchblogEntry(){
    const response=await fetch('https://facebook.github.io/react-native/movies.json');
    console.log(response.json())
}

fetchblogEntry()

Comments

0

Can be done using JSON.stringify function as well

fetch(`https://api.github.com/users/username`)
      .then((response) => response.json())
      .then(setdata);
      console.log(JSON.stringify(data));

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.