【发布时间】:2016-11-19 20:06:53
【问题描述】:
我正在使用 Instagram API 搜索特定标签,然后显示第一个结果的图像。这里我使用JSON.parse() 函数解析从向https://api.instagram.com/v1/tags/{tag}/media/recent?access_token={access token} 发出http get 请求返回的JSON。这是请求返回给我的内容(- 一切都无关紧要)。注意:我删除了部分 URL,假设它们是正确的。
{
"data":[
{
"images":{
"low_resolution":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":320,
"height":320
},
"thumbnail":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":150,
"height":150
},
"standard_resolution":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":640,
"height":640
}
}
}
]
}
这是我用来发出请求和解析 JSON 的代码:
request.get("https://api.instagram.com/v1/tags/" + tag + "/media/recent?access_token=" + accessToken, function(error, response, body)
{
console.log(JSON.parse(body).data.images.standard_resolution.url)
});
});
当我运行这段代码时,它会给我这个错误:
/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52
console.log(JSON.parse(body).data.images.standard_resolution.url)
^
TypeError: Cannot read property 'images' of undefined
at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67)
at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
我认为这可能是因为 console.log() 在 JSON.parse() 之前运行,但 JSON.parse 阻止代码运行其他任何东西。我还检查它是否有我可以使用的回调或承诺,但我没有看到任何东西。
现在我不知道发生了什么。有谁知道可能导致此错误的原因以及我该如何解决?
编辑:
好的,所以我发现data 是一个数据数组,所以我需要将我的代码更改为data[0] 现在的问题是我在运行代码时收到以下错误:
/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52
console.log(JSON.parse(body).data[0].images.standard_resolution.url)
^
TypeError: Cannot read property '0' of undefined
at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67)
at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
【问题讨论】:
-
由于您使 URL 无法使用,也许您可以在问题中放置一个返回 JSON 的示例
-
@Tibrogargan 它已经在第一段文字的下方
-
抱歉,错过了。数据是一个数组。你可能想要 data[0].images
-
正如@Tibrogargan 所说。 data 是一个对象数组,因此要获取您需要的图像
JSON.parse(body).data[0].images -
@pjtnt11 很明显,您在
body中返回的内容与您发布的 JSON 不匹配。这就是为什么发布minimal reproducible example 很重要的原因。没有人可以重现您所看到的。
标签: javascript json node.js instagram-api