我的应用输出[object Object]代替文本字符串-请问如何解决?

时间:2019-06-17 14:29:09

标签: javascript

enter image description here我想从这样的API页面中提取数据:https://api.discogs.com/releases/249504

例如,您可以看到它包含...

"labels": [{"name": "RCA", "entity_type": "1", "catno": "PB 41447"

我想将labels.name和labels.catno读入我的脚本中,以便将该数据输出为.csv

我尝试将标签更改为labels.name,但这没有用,只是造成了严重的错误。

在我的主要scripts.js文件中,......

  const id = data.id;
  const artist = data.artists[0].name;
  var title = data.title;
  var formats = data.formats;
  var labels = data.labels;
  var country = data.country;
  if (country == "") country = "Unknown";
  var released = data.released_formatted;
  if (typeof released == 'undefined') released = "Unknown";
  const genres = data.genres[0];
  var styles = data.styles;
  if (typeof styles == 'undefined') styles = "None";
  let tracklist =''; //sets tracklist to an empty string

[...]

            const id = data.id;
            const artist = data.artists[0].name;
            var title = data.title;
            var formats = data.formats;
            var labels = data.labels;
            var country = data.country;
            if (country == "") country = "Unknown";
            var released = data.released_formatted;
            if (typeof released == 'undefined') released = "Unknown";
            const genres = data.genres[0];
            var styles = data.styles;
            if (typeof styles == 'undefined') styles = "None";
            let tracklist =''; //sets tracklist to an empty string

...但是在我的csv.js文件中,我也有...

const id = data.id
const artists = data.artists ? data.artists.map(artist => artist.name) : []
const title = data.title || []
const formats = data.formats || []
const labels = data.labels || []
        console.log(labels);
const country = data.country || 'Unknown'
const released = data.released_formatted || 'Unknown'
const genres = data.genres || []
const styles = data.styles || []
const tracklist = data.tracklist ? data.tracklist.map(track => track.title) : []

我不确定为什么在一个文件中什么是变量在另一个文件中是常量?也许这需要解决。

我想在我的csv文件中的一列中输入labels.name,在其中一列中输入labels.catno。

1 个答案:

答案 0 :(得分:0)

问题在于您显示的“标签”不是对象。它是一组对象。像labels.namelabels.catno这样的阅读将无法正常工作。

"labels": [{"name": "RCA", "entity_type": "1", "catno": "PB 41447" ...
          ^ Array

将其更改为类似对象

"labels": {"name": "RCA", "entity_type": "1", "catno": "PB 41447" ...

或使用labels[0].namelabels[0].catno应该可以解决您的问题。