首页 > 解决方案 > 通过Javascript读取Object的属性

问题描述

我正在尝试从对象文本中提取价格值。我收到未定义的值。请任何建议。

const fetch = require('node-fetch');

console.log('1. lets start')
fetch('https://api.binance.us/api/v3/avgPrice?symbol=DOGEUSD')
    .then(res => res.text())
    .then(text => {
     
      console.log(text['price']);



    })

标签: javascriptnode.jsarraysobject

解决方案


将 text() 更改为 json(),以获取 json 对象:

const fetch = require('node-fetch');

console.log('1. lets start')
fetch('https://api.binance.us/api/v3/avgPrice?symbol=DOGEUSD')
    .then(res => res.json())
    .then(res => {
      console.log(res['price']);
    })

推荐阅读