【发布时间】:2021-04-21 06:13:54
【问题描述】:
我收到错误:
TypeError: Cannot read property 'url' of undefined
当我像这样循环响应时:
getPhoto = (venueId) => {
const PhotoEndpoint = `https://api.foursquare.com/v2/venues/${venueId}/photos?`;
const parameters = {
client_id: "my client_id",
client_secret: " my client_secret",
group: "venue",
limit: "100",
v: "20211301",
};
axios
.get(PhotoEndpoint + new URLSearchParams(parameters))
.then((response) => {
for (var i = 0; i < response.data.response.photos.items.length; i++) {
this.setState({
photosUrl:
response.data.response.photos.items[i].prefix +
"original" +
response.data.response.photos.items[i].suffix,
});
}
})
.catch((error) => {
console.log(" My photo Error : " + error);
});
};
我收到的数据是这样的: https://foursquare.com/developers/explore#req=venues%2F43695300f964a5208c291fe3%2Fphotos%3F
或者我也把它贴在这里,以防链接失效:
{
data: {
meta: {
code: 200,
requestId: "6002bcf5c8ba5b3c6d668965",
},
notifications: [{
type: "notificationTray",
item: {
unreadCount: 5,
},
}, ],
response: {
photos: {
count: 30,
items: [{
id: "51b8f916498e6a8c16a329eb",
createdAt: 1371076886,
source: {
name: "Instagram",
url: "http://instagram.com",
},
prefix: "https://fastly.4sqi.net/img/general/",
suffix: "/26739064_mUxQ4CGrobFqwpcAIoX6YoAdH0xCDT4YAxaU6y65PPI.jpg",
width: 612,
height: 612,
user: {
id: 26739064,
firstName: "Darius",
lastName: "Alonzo",
gender: "male",
countryCode: "US",
photo: {
prefix: "https://fastly.4sqi.net/img/user/",
suffix: "/BWWLKDFKJ4VOVFXK.jpg",
},
},
checkin: {
id: "51b8f915498e481c2e86de41",
createdAt: 1371076885,
type: "checkin",
timeZoneOffset: -240,
},
visibility: "public",
}, ],
},
},
}
};
会有超过 100 个项目。
我试图让 prefix 将它与 original 的大小连接起来,然后再次连接 suffix 以创建一个 URL,然后 setState我的 PhotoUrl 和后来渲染每张照片,但我得到 TypeError
我做错了什么?
【问题讨论】:
-
您的架构不包含源元素。也许你需要把照片元素的前缀和后缀结合起来。
-
无法重现问题:wandbox.org/permlink/IIIwUJfVoTHFM67x 请提供重现问题的minimal reproducible example。可能有一项没有
source。删除source会产生错误:wandbox.org/permlink/zcat93f0IgKNTmy9 -
即使在您更新后仍然无法重现:wandbox.org/permlink/RX1xsBsh8eZudpBA 请提供minimal reproducible example 错误消息
TypeError: Cannot read property 'url' of undefined在这种情况下毫无意义。你没有访问url。 -
谢谢@Sam,您的回答是正确的,请回答问题,以便我将其指定为正确答案。
-
@Harry9345 很高兴,它帮助了你。我添加了答案,请接受它,以便它可以帮助其他人。
标签: javascript json reactjs axios foursquare