#Altering a function variable from import

7 messages ยท Page 1 of 1 (latest)

placid fable
#

This might be a simple question but I have a page called services (services.astro) and in the frontmatter I had first a check to see which language the browser is running and give me back a variable called $lang with either 'EN' or 'SV'. I then had a GraphQL query where I fetch the content from Wordpress depending on the language. Works great. But while refactoring I've put the entire fetch-request in a client.js file and now that fetch request can't access the $lang variable from services.astro of course. So I'm a little bit stuck on how to handle it. One solution I've come to is to set a $lang variable in client.js fetch request as 'EN' default and then when running the function in services.astro I could add the variable to the call to that function. But I can't seem to get it to work. This is probably just me being worthless at javascript but I'm kind of stuck right now. Could anyone take a look at this and see if my thinking is correct or if I should do it any other way? I'm attaching two screenshots from services.astro and client.js. Thanks in advance!

dim cedar
#

Your getAllServices function currently doesn't take any input.

export const getAllServices = async (lang) => ....
placid fable
dim cedar
#

We've all been there ๐Ÿ™‚

#

Would highly recommend looking into typescript, or letting typescript check your js files as well. Would have caught this error easily ๐Ÿ™‚

placid fable
#

Oh, interesting! I've been curious but not sure where to start. Will definately look into it ๐Ÿ™‚

dim cedar
#

It's quite easy actually. Literally use the .ts file ending instead of .js and it should work