我正在尝试创建一个命令来输出玩家收藏品并总结recentAveragePrice列出的物品总数。问题是当我尝试输出此 API 的任何部分时,它只是输出undefined.API URL: https://inventory.roblox.com/v1/users/1417341214/assets/collectibles ?assetType=Hat&sortOrder=Desc&limit=100if (command === "inv"){ let getInv = async () => { let response = await axios.get("https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100"); let inv = response.data; return inv; } let invValue = await getInv(); console.log(invValue); message.channel.send(`${invValue.data.name} \n ${invValue.data.recentAveragePrice}`);}
1 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
这是因为返回的数据是一个对象数组。如果您想将它们全部作为消息发送,您可以迭代它们。如果您想将它们一一发送,可以使用以下方法:
if (command === 'inv') {
const getInv = async () => {
const response = await axios.get(
'https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?sortOrder=Asc&limit=100',
);
return response.data;
};
const invValue = await getInv();
let total = 0;
invValue.data.forEach((item) => {
message.channel.send(`${item.name} \n ${item.recentAveragePrice}`);
total += item.recentAveragePrice;
});
message.channel.send(`Total average price: ${total}`);
}
结果:
添加回答
举报
0/150
提交
取消