My database holds items that have a generated hash value as the primary key. Although it has good collision tolerance, I want to check to check to make sure a newly generated hash value doesn’t already exist in the table. If one does exist, I generate a new one and try again and if one doesn’t, I just insert the new item with the unique hash. I have all of the other code working fine, its just the checking that isn’t working.
I just want to know why my code block after my .then() doesn’t want to run. Am I using it right?
Is there some reference to that code from loading the Main page/other components actions?
I would start by trying to execute the DB query from the main page, something like the below
Home Page Code:
import wixData from 'wix-data';
$w.onReady(function () {
wixData.query("UserData").then( (results) => {
console.log("DB Query Result - "+ JSON.stringify(results));
});
});
Thanks, that seems to work if I bring everything from the backend to the page’s code. How can I go about fixing it and making it run in the backend? It is the exact same code so I am completely stumped as to why it doesn’t work…
The action needs to be initiated somehow, when running this code block on the onReady action of the Home Page it is being executed as part of the Home Page loading
You could, as Heson suggested above, tie this code to other actions such as initial feeders for components such as tables, text panel, etc’ and even onClicks for components that support it such as buttons
Sorry, I should have been more clear. I am calling the backend module (which contains the code I am talking about) from an onClick event and it is being executed so I know that my linking isn’t the issue. The problem is that, although it is running, I get no results from the query even though the exact same code positioned in the page’s code works just fine.
I have this code block
console.log(“Starting”);
wixData.query(“UserData”)
.eq(“userHashId”, input)
.find()
.then( (results) =>
{console.log("DB Query Result - "+ JSON.stringify(results));
});
console.log(“Ending”);
In my page’s code it gives this a result…
but when moved to the backend and called with the same input I receive…
Still missing some details but I think you might be missing a “return” statement when running in backend code
when running inside a context of a page’s onReady action you are simply resolving the wix-data query promise
when running this code block inside a function as part of backend code, the function should return the promise in order for the consumer to be able to resolve it