【问题标题】:Axios json response is undefinedAxios json 响应未定义
【发布时间】:2021-11-02 14:55:00
【问题描述】:

我正在向 opensea api 发送请求并尝试记录 trait 数据。整个响应的示例在 pastebin url 中。感谢您抽出宝贵时间对此进行审查。 https://pastebin.com/ugQHjbn1

var axios = require("axios").default;

var r = {
    method: 'GET',
    url: 'https://api.opensea.io/api/v1/assets',
    params: { order_direction: 'desc', offset: '0', limit: '1', collection:`enter code here` 'brotchain' }
};

axios.request(r).then(function (response) {
    console.log(JSON.stringify(response.data.assets.token_id));
}).catch(function (error) {
    console.error(error);
});

【问题讨论】:

    标签: javascript node.js arrays json axios


    【解决方案1】:

    你的问题是数据里面的assets是一个数组,基本上你不能做response.data.assets.token_id,因为数组[]上没有token_id

    为了显示每个资产,您需要遍历数组,然后显示资产信息或其中的任何属性。

    这是您的代码,显示每个条目的所有 token_id 及其 traits

    注意:将来查看完整响应并使用此处定义的不同结构采取行动将有所帮助。

    var r = {
      method: 'GET',
      url: 'https://api.opensea.io/api/v1/assets',
      params: {
        order_direction: 'desc',
        offset: '0',
        limit: '1',
        collection: 'brotchain'
      }
    };
    
    axios.request(r).then(response => {
      const assets = response.data.assets;
    
      assets.forEach(asset => {
        console.log("token id of the asset", asset.token_id);
        console.log("now we will show all the traits for this asset")
        console.log(asset.traits);
        console.log("end of the asset");
      })
    }).catch(error => {
      console.error(error);
    });
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    【讨论】:

    • 谢谢你,我希望我以前真的看过这个
    • 不用担心@boat如果是正确答案,你可以标记它!
    • 照做了,谢谢!
    猜你喜欢
    • 2021-05-26
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2018-09-27
    • 2019-06-27
    相关资源
    最近更新 更多