![](/img/trans.png)
[英]Why do I get an 'undefined' error when trying to read a JSON from an URL
[英]How to fix 'when trying to get a url from a json file i get undefined'?
我正在和朋友為個人服務器制作一個機器人,並且嘗試將其連接到xboru的api https://xbooru.com/index.php?page=help&topic=dapi ,問題是在轉換.xml文件之后到.json文件中,當我嘗試獲取file_url時,我得到的結果是不確定的
我已經嘗試了一些事情(我對js相當了解),例如將json轉換為js數組並控制值
xmlToJson(url, function(err, data) { //from xml to json
if (err) {
return console.err(err);
}
let json = JSON.stringify(data, null, 2)
let myArr = JSON.parse(json)
console.log(json)
console.log(myArr)
let post2 = (myArr.posts.post[0])
console.log(post2.sample_url)//gives undefined
message.channel.send(post2.sample_url)//error because i can't send empty message
});
我希望輸出是圖像URL,但是輸出就像我已經說過的“未定義”
找到了答案..當我在做console.log(post2.sample_url)時,我需要添加另一件事,即“ $”,因此它變成console.log(post2。$。sample_url)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.