Skip to content
Advertisement

How to return a derived function that use fetched data from db based on value of another writable value in svelte/svelte kit?

I am trying to get my language table’s data from db in store.js and modify that data based on the language user selected. So the selected language is a writable variable and I need to get a derived function that returns the modified data. Here is my code in store.js

JavaScript

Here is the code in my +page.svelte

JavaScript

I am getting ‘undefined’ data. I am just trying to print out the data variable for testing. Please note that, the console log that I am printing after getting the data from the API endpoint is showing the data successfully. Also, another thing, if I just use a function in store instead of the getTypography function, that returns different static array based on the language chosen, it works perfectly fine. So to my understanding the issue might be getting the data properly from db. What can I do here?

Advertisement

Answer

The derived store makes no sense in this context because the data is updated asynchronously. The problem being that getTypography is not returning anything, but being used in data as if that were the case.

Just make data a writable and update that when the language changes. I.e. use something like:

JavaScript

Where the updateData function is analogous to getTypography but sets the data store.

Something like:

JavaScript

(You may want to add additional logic to invalidate previous requests to prevent race conditions.)


If data for all languages is requested at once, you can update a base store asynchronously and use a derived store just for the filtering. It should be based on both the language (key) and the store that is updated with all the language data.

An example of the principle:

JavaScript

REPL


You could also rewrite getTypography to actually return the promise it creates internally, but then you would have to use something like {#await} everywhere you use data.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement