0

The intention of the JavaScript code below is to fetch data from the Random User Generator, and to print the JSON result into the console. When calling the function after its declaration, it returns as "undefined".

Why is this asynchronous function returning as undefined instead of printing the result of the fetch method to the console?

const getRandomUser = async () => {
  try {
    let res = await fetch("https://api.randomuser.me/?nat=US&results=1");
    let { results } = res.json();
    console.log(results);
  } catch (error) {
    console.error(error);
  }
};

getRandomUser();
crayden
  • 2,130
  • 6
  • 36
  • 65
  • To supplement the answers you can refer [here](https://developer.mozilla.org/en-US/docs/Web/API/Body/json#Return_value) and note the return value of `json()` is a `Promise`. – Marty Sep 06 '20 at 23:10

2 Answers2

2

.json returns a promise object and will need to be awaited to get the value back. This returned

[
  {
    "gender": "male",
    "name": {
      "title": "Mr",
      "first": "Andrew",
      "last": "Alvarez"
    },
    "location": {
      "street": {
        "number": 6490,
        "name": "E North St"
      },
      "city": "El Cajon",
      "state": "Hawaii",
      "country": "United States",
      "postcode": 78991,
      "coordinates": {
        "latitude": "-66.7376",
        "longitude": "-3.0261"
      },
      "timezone": {
        "offset": "-1:00",
        "description": "Azores, Cape Verde Islands"
      }
    },
    "email": "andrew.alvarez@example.com",
    "login": {
      "uuid": "006a343c-98de-45f0-ba0f-fb053be9efb2",
      "username": "angrywolf977",
      "password": "nobody",
      "salt": "JH14v7c8",
      "md5": "8c69fb8a8d65dbbf3cbdb71679b44c9e",
      "sha1": "b03b94155eff0dac5b733d7398a68b2e3f0513b1",
      "sha256": "fb26ce1e4cc7f067c6da9208454a91bda94fc3403119ebfa491a9620ff25de53"
    },
    "dob": {
      "date": "1982-06-30T11:22:22.724Z",
      "age": 38
    },
    "registered": {
      "date": "2002-09-12T21:36:16.737Z",
      "age": 18
    },
    "phone": "(278)-599-6197",
    "cell": "(842)-913-4573",
    "id": {
      "name": "SSN",
      "value": "639-63-2310"
    },
    "picture": {
      "large": "https://randomuser.me/api/portraits/men/36.jpg",
      "medium": "https://randomuser.me/api/portraits/med/men/36.jpg",
      "thumbnail": "https://randomuser.me/api/portraits/thumb/men/36.jpg"
    },
    "nat": "US"
  }
]

const getRandomUser = async () => {
  try {
    let res = await fetch("https://api.randomuser.me/?nat=US&results=1");
    let { results } = await res.json();
    console.log(results);
  } catch (error) {
    console.error(error);
  }
};

getRandomUser();
jmp
  • 2,175
  • 2
  • 17
  • 16
2

You have await res.json() too, so in your case it should end up looking like this:

const getRandomUser = async () => {
  try {
    let res = await fetch("https://api.randomuser.me/?nat=US&results=1");
    let { results } = await res.json(); // Notice the await
    console.log(results);
  } catch (error) {
    console.error(error);
  }
};

getRandomUser();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Cristian
  • 56
  • 2