From 8df35d804bec39bc689886bd8036d5348ade46e1 Mon Sep 17 00:00:00 2001 From: Esvin Ambrocio Date: Thu, 28 Oct 2021 20:06:20 -0600 Subject: [PATCH 1/4] =?UTF-8?q?Soluci=C3=B3n=20al=20ejecutar=201)=20parsea?= =?UTF-8?q?r=20datos=20a=20json=202)=20cambiar=20'4'=20a=204=203)=20instan?= =?UTF-8?q?ciar=20XMLHttpRequest=20dentro=20de=20la=20funci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index d6fa599..3662d43 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,15 @@ var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; var API = 'https://rickandmortyapi.com/api/character/'; -var xhttp = new XMLHttpRequest(); function fetchData(url_api, callback) { + var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function (event) { - if (xhttp.readyState === '4') { - if (xhttp.status == 200) - callback(null, xhttp.responseText); + if (xhttp.readyState === xhttp.DONE) { + if (xhttp.status == 200){ + const respose = JSON.parse(xhttp.responseText) + callback(null, respose); + } else return callback(url_api); } }; From b9f1f5154c491a6e8f7091f967d20ad6f5de61c0 Mon Sep 17 00:00:00 2001 From: Esvin Ambrocio Date: Thu, 28 Oct 2021 20:16:57 -0600 Subject: [PATCH 2/4] =?UTF-8?q?Segunda=20soluci=C3=B3n=20cambiar=20a=20arr?= =?UTF-8?q?ow=20functions=20y=20templeate=20strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 3662d43..972cbd2 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ var API = 'https://rickandmortyapi.com/api/character/'; function fetchData(url_api, callback) { var xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function (event) { + xhttp.onreadystatechange = () =>{ if (xhttp.readyState === xhttp.DONE) { if (xhttp.status == 200){ const respose = JSON.parse(xhttp.responseText) @@ -17,18 +17,18 @@ function fetchData(url_api, callback) { xhttp.send(); }; -fetchData(API, function (error1, data1) { - if (error1) return console.error('Error' + ' ' + error1); +fetchData(API, (error1, data1) => { + if (error1) return console.error(`Error ${error1}`); console.log('Primer Llamado...') - fetchData(API + data1.results[0].id, function (error2, data2) { - if (error2) return console.error(error1); + fetchData(API + data1.results[0].id, (error2, data2) => { + if (error2) return console.error(`Error ${error2}`); console.log('Segundo Llamado...') - fetchData(data2.origin.url, function (error3, data3) { - if (error3) return console.error(error3); + fetchData(data2.origin.url, (error3, data3) =>{ + if (error3) return console.error(`Error ${error3}`); console.log('Tercero Llamado...') - console.log('Personajes:' + ' ' + data1.info.count); - console.log('Primer Personaje:' + ' ' + data2.name); - console.log('Dimensión:' + ' ' + data3.dimension); + console.log(`Personajes: ${data1.info.count}`); + console.log(`Primer Personaje: ${data2.name}`); + console.log(`Dimensión: ${data3.dimension}`); }); }); }); \ No newline at end of file From 7926ab72837a4b2c90943d7719a19f42bc8c25b5 Mon Sep 17 00:00:00 2001 From: Esvin Ambrocio Date: Thu, 28 Oct 2021 21:08:55 -0600 Subject: [PATCH 3/4] tercer problema cambiar el codigo ecmascript(es6) --- package.json | 4 +++- src/index.js | 53 +++++++++++++++++++++++----------------------------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 65a8bf2..112daa6 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,10 @@ "bugs": { "url": "https://github.com/platzi/escuelajs-reto-03/issues" }, + "type": "module", "homepage": "https://github.com/platzi/escuelajs-reto-03#readme", "dependencies": { + "node-fetch": "^3.0.0", "xmlhttprequest": "^1.8.0" } -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 972cbd2..d0f4821 100644 --- a/src/index.js +++ b/src/index.js @@ -1,34 +1,27 @@ -var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +import fetch from 'node-fetch'; +let API = 'https://rickandmortyapi.com/api/character/'; -var API = 'https://rickandmortyapi.com/api/character/'; - -function fetchData(url_api, callback) { - var xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = () =>{ - if (xhttp.readyState === xhttp.DONE) { - if (xhttp.status == 200){ - const respose = JSON.parse(xhttp.responseText) - callback(null, respose); +function fetchData(url_api) { + return fetch(url_api) + .then( response => { + if(response.status == 200){ + return response.json() } - else return callback(url_api); - } - }; - xhttp.open('GET', url_api, false); - xhttp.send(); + console.log(response) + }) + .catch(error => { + console.error(error) + }); }; -fetchData(API, (error1, data1) => { - if (error1) return console.error(`Error ${error1}`); - console.log('Primer Llamado...') - fetchData(API + data1.results[0].id, (error2, data2) => { - if (error2) return console.error(`Error ${error2}`); - console.log('Segundo Llamado...') - fetchData(data2.origin.url, (error3, data3) =>{ - if (error3) return console.error(`Error ${error3}`); - console.log('Tercero Llamado...') - console.log(`Personajes: ${data1.info.count}`); - console.log(`Primer Personaje: ${data2.name}`); - console.log(`Dimensión: ${data3.dimension}`); - }); - }); -}); \ No newline at end of file +async function getData (){ + const first = await fetchData(API); + const urlSecond = `${API}${first.results[0].id}`; + const second = await fetchData(urlSecond); + const urlThird = `${second.origin.url}`; + const third = await fetchData(urlThird); + console.log(`Personajes: ${first.info.count}`); + console.log(`Primer Personaje: ${second.name}`); + console.log(`Dimensión: ${third.dimension}`); +} +getData() \ No newline at end of file From 7ce5c619c03803eb92edde115e0bed91e53dd290 Mon Sep 17 00:00:00 2001 From: Esvin Ambrocio Date: Thu, 28 Oct 2021 21:14:22 -0600 Subject: [PATCH 4/4] agregar punto y coma a la ultima linea --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index d0f4821..7f87f48 100644 --- a/src/index.js +++ b/src/index.js @@ -24,4 +24,4 @@ async function getData (){ console.log(`Primer Personaje: ${second.name}`); console.log(`Dimensión: ${third.dimension}`); } -getData() \ No newline at end of file +getData(); \ No newline at end of file