Repeater not displaying records when querying a field with a GUID id

Hi.

I have two collections.

  1. astrouserobjects - this contains objects (Astro objects) that have been seen by astronomers who are users of my website. Each object has a unique id (a GUID) that is stored in the collection’s hidden _Id field.

  2. astrouserobjectsimages - this contains images and image details that these astronomers (users on my site) have seen for the Astro Objects in the astrouserobjects collection. A record for each image seen. In this collection, there is a astroUserObjectId field. This field is a text field and stores the id, a GUID, of the astronomer object that is stored in the _Id field in the astrouserobjects collection.

Here’s an example of a user object id:

41aa2a4c-d4a0-4d3e-a415-e1208123c409

I have a dynamic item page for each user’s Astro Object. On this page, I have a Repeater that is populated by a dataset, datset4, of the astrouserobjectsimages collection. This displays all the images the user has taken for this specific Astro object.

On this page in code, I retrieve the id of the user’s Astro object from the astrouserobjects collection via currentObject._id and store it in a variable, UserObjectId (a String). I can see in the Console that the id of the user’s Astro object is retrieved and stored in my variable. Part of the code looks like this:

$w('#dynamicDataset').onReady(() => {
currentObject = $w('#dynamicDataset').getCurrentItem()
//userobject id
UserObjectId = currentObject._id;

On the same page, I have created a function to query the astrouserobjectsimages collection dataset by the variable, UserObjectId, using the .eq function. This is part of the code:

$w("#dataset4").setFilter(wixData.filter()
.eq("astroUserObjectId",UserObjectId)

However, this does not return any records and therefore no records are displayed in the Repeater. Strangely, if I hardcode the user’s object id in the query like this:

.eq("astroUserObjectId","41aa2a4c-d4a0-4d3e-a415-e1208123c409")

the records are returned and displayed in the Repeater.

Any idea on how to resolve this?

Many thanks

Mark

Try this one…


import wixData from 'wix-data';

$w.onReady(()=> {console.log('page ready...');
	$w('#dynamicDataset').onReady(()=> {console.log('Dynamic-Dataset ready...');
		let curItem = $w('#dynamicDataset').getCurrentItem(); console.log('Current-Item: ', curItem)
		let userID = curItem._id; console.log('User-ID: ', userID);
		$w("#dataset4").onReady(()=>{console.log('Dataset-4 ready...');
			$w("#dataset4").setFilter(wixData.filter().eq("astroUserObjectId", userID));
		});
	});
});

Many thanks for this it worked.

One last thing. I would like to have the userID variable available to other functions on my page. Currently, I am having to put this part of your code in each function to get the UserId. If I add, Let UserId at the top of my page the UserId variable is undefined in all the other functions.

Any ideas?

$w('#dynamicDataset').onReady(()=> {console.log('Dynamic-Dataset ready...');
		let curItem = $w('#dynamicDataset').getCurrentItem(); console.log('Current-Item: ', curItem)
		let userID = curItem._id; console.log('User-ID: ', userID);
		$w("#dataset4").onReady(()=>{console.log('Dataset-4 ready...');
			$w("#dataset4").setFilter(wixData.filter().eq("astroUserObjectId", userID));
		});

Many thanks

Mark

There is a difference on how to define your VARIABLE.

  1. scoped variable
  2. global variable

A scoped variable will work only in a defined area for example inside a function only.

function myFunctionX() {
let myScopedVariable = ‘zzzzzz’;
}

On other hand you can define a GLOBAL-VARIABLE like…

let curItem;

$w.onReady(()={
    $w('#dynamicDataset').onReady(()=> {console.log('Dynamic-Dataset ready...');
        curItem = $w('#dynamicDataset').getCurrentItem();
    });
});

Instead of a VARIABLE, you probably could also use a CONSTANT…

const curItem = $w(‘#dynamicDataset’).getCurrentItem();