How can I access the values of a URL-type json?

0

I'm getting the values of this json link but, when I try to get "films" it shows me the value as undefined . I refer to this exact value but I think it is not recognizing that it is a json and that is why it shows the undefined.

The reference is to this value

"films": [
        "https://swapi.co/api/films/2/", 
        "https://swapi.co/api/films/6/", 
        "https://swapi.co/api/films/3/", 
        "https://swapi.co/api/films/1/", 
        "https://swapi.co/api/films/7/"
    ], 

Annex the code

function get (URL, callback) {
    "use strict";
    
    const xhttp = new XMLHttpRequest();
    
    xhttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
        //Todo ok
        callback(null, JSON.parse(this.responseText));
      } else {
        //Hubo un error
        callback(new Error('Se produjo un error al realizar el request: ${this.status}')); //this apunta al request xhttp
      }
    }
  
    xhttp.open('GET', URL); //Type of Request
    xhttp.send(null);  // Send request to server
  }
  

  function handleError(err) {
    console.log('Request failed: ${err}');
  }

  get('https://swapi.co/api/people/1/', function onResponde(err, luke) {
    if (err) return handleError;
    /*Trae el mundo*/
    get(luke.homeworld, function onHomeWorldResponse (err, homeworld) {
      if (err) return handleError;
      /*Traer la especie*/
      get(luke.species, function onSpeciesResponse (err, species) {
        if (err) return handleError;
        get(luke.films[0], function onFilmsResponse(err, films) {
          if (err) return handleError;
          luke.films[0] = films;
          console.log('${luke.name} estuvo en las peliculas ${luke.films.title}');
        })
        luke.species = species;
        console.log('${luke.name} es un ${luke.species.name}');
      })
      luke.homeworld = homeworld;
      console.log('${luke.name} nació en ${luke.homeworld.name}');
    })
    console.log('Request succeded');
    console.log('luke', luke);
    console.log("Datos de Sr." + luke.name.substr(0,4) + ":");
    console.log('Nombre Completo: ${luke.name}
                 Género: ${luke.gender}
                 Estatura: ${luke.height}
                 Color de Cabello: ${luke.hair_color}
                 Color de ojos: ${luke.eye_color}');
  });
    
asked by Arnell Vasquez C 23.02.2018 в 19:54
source

2 answers

0

You get that error because you refer to a property that does not have ( title ) what you must do first is to recover each film with the Get function that you do iteratively for this case use a for that retrieves the information and prints it can make the change so that it only recovers it and then at the end concatenate the names of the movies.

function get (URL, callback) {
    "use strict";
    
    const xhttp = new XMLHttpRequest();
    
    xhttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
        //Todo ok
        callback(null, JSON.parse(this.responseText));
      } else {
        //Hubo un error
        callback(new Error('Se produjo un error al realizar el request: ${this.status}')); //this apunta al request xhttp
      }
    }
  
    xhttp.open('GET', URL); //Type of Request
    xhttp.send(null);  // Send request to server
  }
  

  function handleError(err) {
    console.log('Request failed: ${err}');
  }

  get('https://swapi.co/api/people/1/', function onResponde(err, luke) {
    if (err) return handleError;
    /*Trae el mundo*/
    get(luke.homeworld, function onHomeWorldResponse (err, homeworld) {
      if (err) return handleError;
      /*Traer la especie*/
      get(luke.species, function onSpeciesResponse (err, species) {
        if (err) return handleError;
       
        for(var i=0;i < luke.films.length ;i++) //for que recupera la informacion 
        get(luke.films[i], function onFilmsResponse(err, films) {
          if (err) return handleError;
          luke.films[i] = films; //recupera la informacion del film y la asigna al index correcto
          
          console.log('${luke.name} estuvo en las peliculas ${luke.films[i].title}'); // muestra el titulo del film recuperado.si desea obvias esta parte
        })
// En esta linea ya tienes todos los films recuperados 
        luke.species = species;
        console.log('${luke.name} es un ${luke.species.name}');
      })
      luke.homeworld = homeworld;
      console.log('${luke.name} nació en ${luke.homeworld.name}');
    })
    console.log('Request succeded');
    console.log('luke', luke);
    console.log("Datos de Sr." + luke.name.substr(0,4) + ":");
    console.log('Nombre Completo: ${luke.name}
                 Género: ${luke.gender}
                 Estatura: ${luke.height}
                 Color de Cabello: ${luke.hair_color}
                 Color de ojos: ${luke.eye_color}');
  });
    
answered by 23.02.2018 / 20:33
source
0

is undefined because it is an array, you should go through the array to get all the movies, but for the example I think you want to get the first .-

then you must change

console.log('${luke.name} estuvo en las peliculas ${luke.films.title}');

for

console.log('${luke.name} estuvo en las peliculas ${luke.films[0].title}');

greetings

    
answered by 23.02.2018 в 20:49