Load function is not sending updated object to the front end, a value fetched from database #11859
Unanswered
elhananjair
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Shouldnt you code be like this? // these variables are global and will not reset after every query,
// they will persist for the whole time your server runs.
// If you do not want that, you should put these in the overAllResults function and use them there.
let queryResult = null;
let keys = ['k1', 'k2', 'k3']
let counter = 0
let data = {
k1: 0,
k2: 0,
k3: 0,
}
async function queryDB(){
return await sql.begin(sql => {
//multiple sql queries and it returns an array of results
})
}
// updated
async function overAllResult() {
let resData = {...data};
queryResult = await queryDB();
queryResult.forEach(result => {
resData[keys[counter]] = result[0].count //Count is the key from query result
});
return resData;
}
// updated
export const load = async ({locals}) => {
const results = await overAllResult()
return {
results
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello guys,
I have multiple Postgres queries that I execute when the user hits refresh for an update. I store fetched data from the database into an object I have created and then put that inside the return.
But what I am facing is, that unless I restart the application when I refresh the page it still shows the old values of the sent object. Is there any way to avoid that or any issue with my code, this is what my code looks like:
Beta Was this translation helpful? Give feedback.
All reactions