阿晨1998
2024-01-18 14:54:04
我正在嘗試創建一個命令來輸出玩家收藏品并總結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
提交
取消