-1

我正在研究 coinmarketcap.com api,我只需要名称和价格并保存到 Mongo。它正在工作,但最终返回了未定义的值。价格值在报价之下。怎么了 ?

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);
  }
}
);
4

4 回答 4

1

您不应该将其包装在另一个中Promise(请参阅:什么是显式承诺构造反模式以及如何避免它?),只需创建一个异步方法

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;
  }
}
于 2022-02-14T09:40:05.767 回答
0

quote包含由它们的键标识的价格,如下所示:

"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"
                }
            }

所以为了解决这个错误,你需要在这里指定正确的密钥(例如美元):

我测试了这段代码,它可以工作,请将其复制到单独的 js 文件中并检查结果(替换您的 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()
于 2022-02-14T09:40:38.853 回答
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 
}
于 2022-02-14T09:49:50.053 回答
0

在 quote.USD 或 quote['USD'] 中获取价格,还需要使用地图返回名称和价格对象。

所以试试这个:

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': '5345bcb4-0203-4374-bef6-d67a89a6d216',
            },
        });
        if (response) {
            const coin = response.data;
            cointData = coin.data.map(a => ({ name: a.name,  price: a.quote.USD.price }))
            resolve(cointData);
        }
    } catch (ex) {
        response = null;
        console.log(ex);
        reject(ex);
    }
});
于 2022-02-14T15:09:18.253 回答