Home > OS >  How to set a svelte store to an object array
How to set a svelte store to an object array

Time:09-08

Hello I am requesting a json file from a flask server, after processing the response I know have the json file showing up in the console.log. The json file contains and array of objects of type advisor:

export type advis = {
    Advisory: string;
    Teacher: string;
    Room: string;
};

I am not sure if I am completely understanding svelte stores but I initialized the store with an empty array of these objects.

let advisRoomInit: advis[] = [];
// Init data stores
export const advisoryRooms = writable(advisRoomInit);

After getting the data back I then try and set the res from the array to the store like so

onMount(
        async () => {
            try {
                const ad = await request(AdvisoryEndPoint); // Now this will wait till it finished
                let AD : advis[] = ad
                console.log(AD)
                advisoryRooms.set(AD)
                console.log(advisoryRooms);
            } catch (e) {
                console.log(e);
            }
        }
)

While the first console.log(AD) returns the object array while the result of printing the just set store value returns {}

If anyone could help me in setting my object array to svelte store that would be very helpful as I have a deadline at 7:55 tmr morning and it is currently 23:00, I am the first red bull in.

CodePudding user response:

console.log(advisoryRooms);

will print the store object. Prepend a $ to access the store value

console.log($advisoryRooms);

docs: "Prefix stores with $ to access their values"

  • Related