Tim MG
Tim MG

Reputation: 504

Can't get data from a json object

I'm trying to console.log the "name" of the object. Everytime i try this it returns "undefined". How can i solve this?

    fetch('http://localhost:9000/quiz/code', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            name: code
        })
    }).then(res => res.json())
        .then(data => {
            data = JSON.stringify(data);
            console.log("data: " + data);
            console.log("name: " + data.name);
        })

This is the output of my first console.log:

data: [{"_id":"5cab356c8b35014074855ada","name":"fsgsdfgd","vragen":{"vragen":[{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"},{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"}]}}]

This is the output of my second console.log:

name: undefined

Upvotes: 1

Views: 1622

Answers (5)

user14742098
user14742098

Reputation:

//try like this
success: function (result) {
      console.log(result);
      show_edit_form();
      $('#txt_order_id').val(result[0].rec_id);
      $('#txt_first_name').val(result[0].fname);
      $('#txt_last_name').val(result[0].lname);
 },

Upvotes: 0

Jackkobec
Jackkobec

Reputation: 6705

data: [{" is array

You should access to the array element before you get property by name:

Example access by index:

console.log("name: " + data[0].name);

Upvotes: -2

Sarah Groß
Sarah Groß

Reputation: 10879

You are using stringify() on the response data and then you treat it as an object. That cannot work. Just remove the data = JSON.stringify(data); as you already have valid JSON (in the form of an array). You can simply loop over the objects in the array and get each object's name:

fetch('http://localhost:9000/quiz/code', {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        name: code
    })
}).then(res => res.json())
    .then(data => {
        console.log("data: ", data);
        data.forEach(function(obj)  {
            console.log("name: " + obj.name);
        });
        console.log("name: " + data[0].name);
    })

Upvotes: 4

Tim MG
Tim MG

Reputation: 504

I removed the JSON.stringify(data) and added data[0].name to my console.log. This solved the problem. My new code looks like this:

fetch('http://localhost:9000/quiz/code', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            name: code
        })
    }).then(res => res.json())
        .then(data => {
            data = data;
            console.log("name: " + data[0].name);
        })

Upvotes: 2

kr153
kr153

Reputation: 282

remove the stringify and use data[0].name

data = [{"_id":"5cab356c8b35014074855ada","name":"fsgsdfgd","vragen":{"vragen":[{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"},{"question":"Hoe wordt een middagdutje zoals dit bijvoorbeeld in Spanje wordt gehouden genoemd?","answer":"Een siësta","category":"Algemeen"}]}}]

console.log(data[0].name)

Upvotes: 1

Related Questions