#API JSON discord.js (undici)

21 messages · Page 1 of 1 (latest)

floral dome

Hello, whenever I try to get API Json using the discord.js guide (using undici)
I keep getting undefined value

    const { request } = require("undici");

    const goldW = await request(`${apiURLW}`);
    const { listW } = await goldW.body.json();
    console.log(listW);
lunar tendonBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
thick night

What‘s apiURLW defined as?

floral dome

https://albion-online-data.com/api/v2/stats/gold?count=1

thick night

That api doesn’t return an object with a listW property it seems

Only an array containing one object with price and timestamp property

floral dome

How can I access it ?

I tried to do console.log(listW[0]) yet it gave an error

thick night
thick night
floral dome
floral dome

An Array with an object inside

thick night

You already do get access to it. You just need to not destructure from it

merry jasperBOT

mdn Destructuring assignment
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

thick night

Don’t do that👆

Instead assign without destructuring

floral dome

you mean I have to remove this line const { listW } = await goldW.body.json();

I tried to do this :

    const goldW = await request(`${apiURLW}`);
    console.log(goldW[0].price);

it gave undefined

thick night

You still have to .body.json(), but assign it to a variable like const listW = without the {} destructure