wixData.Query exclusively returns 'undefined' value

Hi,

I am trying to query my database but it doesn’t seem to get any results. It never reaches any of my console.log() statements inside the .then().

export function LoadHashValue(input)
{
console.log("Input is: " + input);
input.trim();

wixData.query("UserData") 
.eq("userHashId", input) 
.find() 
.then( (results) =>  
{ 
	console.log(results.items.length()); 
	if (results.items.length === 0)  
	{ 
		console.log ("Not Found");	 
	} 
	//Success 
	let item = results.items[0].value; 
	console.log(item); 
}).catch( (err) =>  
{ 
	//Error 
	let errorMsg = err; 
	console.log("Error"); 
}); 

}

Also, even if I do a general query on the database, with no specifications, I still get no results.

wixData.query(“UserData”).then( (results) => {
console.log(results.items);
} );

which is strange because I can insert to the same database fine enough. Any help would be appreciated!

Thanks

Hi Lewis,
I think you miss a code to get the data, since I don’t know what elements you want table or repeater.

Generally,
You may add a code after sorting.

If table,
$w(“#table”).rows = results.items;

If repeater,
$w(“#repeater”).data = results.items;

Good luck,
Heson

Hi, thanks for the reply.

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?

Hi,

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…

Hi,

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

Example:

Page Code:

import {logRecord} from 'backend/aModule.jsw';

$w.onReady(function () {
	const userEmail = "someUser@test.com"
	logRecord(userEmail)
});

backend/aModule.jsw:

import wixData from 'wix-data';

export function logRecord(userEmail) {
   return wixData.query("Members")
                 .eq("email", userEmail) 
                 .find()
                 .then( (results)=> {
                        console.log("record - " + JSON.stringify(results)); 
                  }).catch( (err) => {
    			console.log("Ooopsi... - " + err); 
                  });
}

Thanks
Adi

Hi Adi,

Thank you so much, you are a lifesaver :slight_smile: it was just a return statement that I was missing…

Thanks again,
Lewis

Adi, what if I want to get data from logRecord in your above example and do further processing. This somehow doesn’t work at my end:

import {logRecord} from ‘backend/aModule.jsw’;

$w.onReady(function () {
const userEmail = “someUser@test.com” ;
let aRecord = logRecord(userEmail);

    console.log(JSON.stringify(aRecord)); 

});

I get {} in the console - what should I do for the value to get the results from the query in logRecord to get stored in aRecord?

Thanks.