I have an export function calling to the Public Files that looks like this:
export function myJourney(digitalClientName) { let myjourneyPic = '' let myjourneySubTitle = '' let myjourneyAltText = '' return wixData.query("Items1") .eq("title", digitalClientName) .limit(1) .find() .then( (results) => { if(results.items.length > 0) { let myjourneyPic = results.items[0].image.url let myjourneySubTitle = results.items[0].subtitle.text let myjourneyAltText = results.items[0].altText.text console.log(results) } else { console.log(results.items[0]) } return [ myjourneyPic, myjourneySubTitle, myjourneyAltText ] }) }
I then have the front end using the return results, like this:
await myJourney('digitalclientName') .then((Pic, SubTitle, AltTitle) => { $w('#myjourneyPic').background.src = Pic, $w('#subTitle').text = SubTitle, $w('#altTitle').text = AltTitle; }) if (($w('#myjourneyPic').background.src) === '') { $w('#myjourneyPic').background.src = "wix:image://v1/11062b_216a7c5a8f2e4ea0899a27af0efac648~mv2.jpg/_.jpg#originWidth=3360&originHeight=2240" $w('#image16').hidden };
I'm trying to debug and can't seem to find why the export function seems to be returning nulls on each item. Any ideas? Help much appreciated!
Thank you, Bruno! I will insert await...get back to you on results. Your assistance is much appreciated.
Absolutely!
In the editor, the err and says:
"Property 'items' does not exist on type 'Promise<WixDataQueryResult>
Thank you for the details! I tried your version of the backend, and the following syntax showed errors on: const { items }
However, I have checked what my code is doing and if I pass the same variables to a session.setItems set of variables, it seems to work fine. The url and text values of the variables make it to the front end by session storage.
So, my code obviously works doing it this method, but I'm wondering why the same passing of the variable value back through the return function is not working? Any other thoughts are appreciated.
You are returning an array of empty values because of the scope of the variables you created.
BACKEND
export async function myJourney(digitalClientName) { try { const query = await wixData .query('Items1') .eq('title', digitalClientName) .limit(1) .find() const { items } = query || {} return items ? items.length > 0 ? [items[0].image.url, items[0].subtitle.text, items[0].altText.text] : [false, false, false] : [false, false, false] } catch (error) { console.error(error) return [false, false, false] } }
FRONTEND
import { myJourney } from 'backend/myJourney' const DEFAULT_BG = 'wix:image://v1/11062b_216a7c5a8f2e4ea0899a27af0efac648~mv2.jpg/_.jpg#originWidth=3360&originHeight=2240' const DEFAULT_SUBTITLE = 'My journey' const DEFAULT_ALTTEXT = 'My journey' $w.onReady(async () => { // Get the digital client name and insert in myJourney function. const [myJourneyPic, myJourneySubTitle, myJourneyAltText] = await myJourney( 'myJourney' ) if (myJourneyPic === false) console.log('No myJourney pic or error, check WIX Logging') $w('#myjourneyPic').background.src = myJourneyPic || DEFAULT_BG $w('#subTitle').text = myJourneySubTitle || DEFAULT_SUBTITLE $w('#altTitle').text = myJourneyAltText || DEFAULT_ALTTEXT myJourneyPic ? $w('#image16').show() : $w('#image16').hide() })
Can you console.log(results.items[0])?
It looks like it should be results.items[0].image instead of results.items[0].image.url