0

I'm trying to make a command that outputs players collectibles and summarizes the total recentAveragePrice of the listed items. The problem is when I'm trying to output any part of this API, it just outputs undefined.

API URL: https://inventory.roblox.com/v1/users/1417341214/assets/collectibles?assetType=Hat&sortOrder=Desc&limit=100

if (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}`);
}
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Arti
  • 25
  • 4

1 Answers1

1

It's because the returned data is an array of objects. If you want to send them all as a message, you can iterate over them. If you want to send them one by one, the following will work:

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}`);
}

Result:

enter image description here

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57