Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
¿Cómo puedo obtener valor en [Objeto]?

Estoy trabajando en la API de coinmarketcap.com y solo necesito el nombre y el precio y guardarlo en Mongo. Está funcionando pero al final se devolvió un valor indefinido. El valor del precio está debajo de la cotización. Qué está mal ?

 const axios = require('axios'); let response = null; new Promise(async (resolve, reject) => { try { response = await axios.get('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=2', { headers: { 'X-CMC_PRO_API_KEY': 'myKey', }, }); } catch(ex) { response = null; console.log(ex); reject(ex); } if (response) { const coin = response.data; console.dir(coin, { depth: null }); console.log(coin.data.map(a => { a.name, a.quote.price})); resolve(coin); } } );
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

No debería estar envolviendo esto en otra Promise (ver: ¿Qué es el antipatrón de construcción de promesa explícita y cómo lo evito? ), solo haga un método que sea asíncrono

 async function getCoin(){ try { const response = await axios.get('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=2', { headers: { 'X-CMC_PRO_API_KEY': 'YOURKEY', }, }); const coin = response.data; console.dir(coin, { depth: null }); return coin; } catch(ex) { console.log(ex); throw ex; } }
about 4 years ago · Juan Pablo Isaza Report

0

quote contiene precios identificados por sus claves así:

 "quote": { "USD": { "price": 42177.91536878145, "volume_24h": 18068350418.6717, "volume_change_24h": 8.1323, "percent_change_1h": -0.02144759, "percent_change_24h": -0.48235688, "percent_change_7d": -0.65364542, "percent_change_30d": -1.87762517, "percent_change_60d": -13.80076009, "percent_change_90d": -30.24448455, "market_cap": 799599134284.9932, "market_cap_dominance": 42.7605, "fully_diluted_market_cap": 885736222744.41, "last_updated": "2022-02-14T09:35:00.000Z" } }

por lo tanto, para resolver el error, debe especificar la clave adecuada aquí (por ejemplo, USD):

Probé este código y funciona, cópielo dentro del archivo js separado y verifique el resultado (Reemplace su clave API):

 const axios = require('axios'); let response = null; async function getCoin(){ try { const response = await axios.get('https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=2', { headers: { 'X-CMC_PRO_API_KEY': 'KEY', }, }); const coin = response.data; const result = coin.data.map(a => ({ name: a.name, price: a.quote['USD'].price})) console.log(result) return coin; } catch(ex) { console.log(ex); throw ex; } } getCoin()
about 4 years ago · Juan Pablo Isaza Report

0

const axios = require("axios"); async function fetchData() { try { const response = await axios.get( "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=2", { headers: { "X-CMC_PRO_API_KEY": "myKey", }, } ); return [response, null]; } catch (error) { return [null, error]; } } // in your calling function code base const [response,error] = await fetchData() if(response){ // handle response object in calling code base const coin = response.data } if(error){ // handle error explictly in calling code base }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!