Category: Appsmith Support
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

DB queries and JS object set to be run on page load not working

Issue

I am having trouble with DB queries that are supposed to run on page load in my application. Even running them inside a JS object on page load did not work.

Resolution

The solution to the issue of DB queries not running on page load was to check if the necessary dependencies were properly installed and imported. The user was advised to first confirm that the database connection was correctly established and that the queries were written correctly. It was also suggested to use asynchronous functions or promises to handle any delays in the data retrieval process. Additionally, the code should be executed after the DOM has loaded completely to ensure that all elements are available. The user was provided with some sample code for loading data on page load:

window.addEventListener("load", async function() {
try {
const data = await fetch('your-api-url');
const response = await data.json();
// handle response
} catch (error) {
console.error(error);
}
});

Finally, the user was advised to use the console to debug their code and check for any errors or missing dependencies that may be preventing the queries from running.