I ran a query in a function and would like to return the entire query object to the calling function so I can work with various parts of the results. Here’s my code. The rslts variable shows undefined. What am I doing wrong?
import wixData from ‘wix-data’ ;
export function btnCheckOut_click ( event ) {
let rslts = checkAvailable ();
console . log ( rslts );
}
function checkAvailable ( ) {
wixData . query ( “ISBN” )
. eq ( "title" , $w ( "#txtTitle" ). text )
. find ()
. then (( results ) => {
**if** ( results . items . length > 0 ) {
//console.log(results.items);
**return** results ;
} **else** {
$w ( "#txtMsg" ). text = "Not Found" ;
}
})
. catch (( err ) => {
console . log ( err );
});
}
Try this one…
import wixData from 'wix-data';
$w.onReady(() => {console.log("Page is ready...");
$w('#btnCheckOut').onClick(async()=>{
let value = $w("#txtTitle").text;
let res = await checkAvailable(value);
});
});
function checkAvailable(value) {
wixData.query("ISBN")
.eq("title", value)
.find()
.then((results) => {
if(results.items.length > 0) {return results.items;}
else {$w("#txtMsg").text = "No DATA Found";}
}).catch((err)=> {console.log(err);});
}
You have first to get the value, to be able to work with.
And also you will have to use ASYNC-AWAIT or THEN(()=>{}), to be able to let the code work ASYNCHRONOUS / SYNCHRONOUS!
IMPROVE YOUR CODING SKILLS, by reading the following post…
Thanks Velo-Ninja,
Still getting an “undefined” as a return from “checkAvailable.” Any ideas?
What is your current code?
Please paste it here (not as pic).
Show the related DB-FIELD inside your DATABASE (screenshot).
This is a Dynamic Page.
Here’s the db (ISBN) that the data is written to:
Here’s the code for the Dynamic Page:
import { fetch } from ‘wix-fetch’ ;
import wixWindow from ‘wix-window’ ;
import { session } from ‘wix-storage’ ;
import wixData from ‘wix-data’ ;
$w . onReady ( function () {
$w ( “#imgBookImage” ). hide ();
$w ( “#btnCheckOut” ). onClick ( async () => {
let val = $w ( “#txtTitle” ). text ;
let res = await checkAvailable ( val );
console . log ( res );
});
});
// Get the book information from Google API
function getBookDetails ( ibn ) {
// Query the book database by ISBN code.
const url = “https://www.googleapis.com/books/v1/volumes?q=isbn:” + ibn ;
//console.log('URL is ’ + url);
return fetch ( url , { method : ‘get’ })
. then ( response => response . json ())
}
export function page1_viewportEnter ( event ) {
//console.log(‘ISBN: ’ + $w(’#txtISBN’).text);
getBookDetails ( $w ( “#txtISBN” ). text )
. then ( data => {
$w ( “#imgBookImage” ). src = data . items [ 0 ]. volumeInfo . imageLinks . thumbnail . replace ( “http” , “https” );
$w ( “#imgBookImage” ). show ();
}, error => {
console . error ( 'Error: ’ + error . message );
});
}
function checkAvailable ( val ) {
wixData . query ( “ISBN” )
. eq ( “title” , val ) //$w(“#txtTitle”).text)
. find ()
. then (( results ) => {
if ( results . items . length > 0 ) {
//console.log(results.items);
return results . items ;
} else {
// handle case where no matching items found
$w ( “#txtMsg” ). text = “Not Found” ;
}
})
. catch (( err ) => {
console . log ( err );
});
}
$w ( ‘#txtLocation’ ). text = session . getItem ( “oldLocation” );
}
I can’t recognize any errors.
import {fetch} from 'wix-fetch';
import wixWindow from 'wix-window';
import {session} from 'wix-storage';
import wixData from 'wix-data';
let myDatabaseID = "ISBN";
let myDbField = "title";
$w.onReady(function () {
$w("#imgBookImage").hide();
$w("#btnCheckOut").onClick(async() => {
let myValue = $w("#txtTitle").text; console.log("My text-value: ", myValue);
let res = await checkAvailable(myValue); console.log("Result: ", res);
});
});
function checkAvailable(myValue) {
wixData.query(myDatabaseID)
.eq(myDbField, myValue)
.find()
.then((results) => {
if(results.items.length > 0) {
console.log("Result-Items: ", results.items);
return results.items;
}
else {
// handle case where no matching items found
$w("#txtMsg").text = "Not Found";
}
})
.catch((err)=> {console.log(err);});
$w('#txtLocation').text = session.getItem("oldLocation");
}
// Get the book information from Google API
function getBookDetails(ibn) {
// Query the book database by ISBN code.
const url = "https://www.googleapis.com/books/v1/volumes?q=isbn:" + ibn;
//console.log('URL is ' + url);
return fetch(url, {method: 'get'})
.then(response => response.json())
}
export function page1_viewportEnter(event) {
console.log('ISBN: ' + $w('#txtISBN').text);
getBookDetails($w("#txtISBN").text)
.then(data => {
$w("#imgBookImage").src = data.items[0].volumeInfo.imageLinks.thumbnail.replace("http","https");
$w("#imgBookImage").show();
}, error => {
console.error('Error: ' + error.message);
});
}
Run it and pay ATTENTION on the console-logs.
You have all PERMISSIONS in your DATABASE ?
Your DATABASE-ID is right?
What are the console-logs for…?
console.log("My text-value: ", myValue);
console.log("Result: ", res);
console.log("Result-Items: ", results.items);
console.log('ISBN: ' + $w('#txtISBN').text);
Thanks velo-ninja,
I found the answer. I stumbled on another post that said you had to return all parts of the query to get the correct return passed to the calling function. This seems to work. I don’t quite understand why, but I guess it has to do with the async nature of the code. Do you agree?
OI also get "Property … does not exist on type ‘void’ but it seems to work anyway. All the properties are visible in the calling function. Can I get rid of that error or should I just not worry about it? There isn’t any way of declaring the return object is there?
Thanks.
function checkAvailable ( myValue ) {
return wixData . query ( myDatabaseID )
. eq ( “title” , myValue ) //$w(“#txtTitle”).text)
. find ()
. then (( results ) => {
if ( results . items . length > 0 ) {
console . log ( "Result-Items: " , results . items );
return results . items ;
} else {
$w ( “#txtMsg” ). text = “Not Found” ;
}
})
. catch (( err ) => {
console . log ( err );
});
}
return wixData . query ( myDatabaseID ) ← that was on my mind, but i was not sure. So i can’t explain it.
OI also get "Property … does not exist on type ‘void’
Try to use the console and figure out at which point exavtly you get this error.
You have your project infront of your eyes, for you it is easier to do the debugging.
But, i think you are now a step closer to your FULL-SOLUTION.