Looping through dataset

Hey, I want to use a while loop to go through my database and grab each items “location” which will then be sent to determine if it is the closest store to the person searching. I already am able to do the comparison between two locations using a Google API, I just need it now to loop through the database and compare with each stores location, then retrieve the closest location and provide the user with just their information.

How do I set the index to the next item in my dataset at the end of my while loop?

Figured it out, instead of a while loop I just grabbed the size of the dataset and did a for loop. Then using getItems(i,1) retrieved each one for comparison individually. May be able to fix it up a bit more though, since I could just make a single call to the database which grabs all values with getItems and just search through it has an object.

Here’s my code anyway (Purely for anyone else having a similar issue):

export function vectorImage1_click(event, $w) {

  $w("#dealerDataset").onReady( () => { 
    let itemIndex = $w("#dealerDataset").getCurrentItemIndex(); 
    let hasNext = $w("#dealerDataset").hasNext(); // true 
	let location = $w("#dealerDataset").getCurrentItem(); 
	let count = $w("#dealerDataset").getTotalCount(); 
	console.log("Size of the Dataset: ", count); 

	for(var i = 0; i < count; i++){ 

	$w("#dealerDataset").getItems(i,1) 
	  .then( (result) => { 
	    let items = result.items; 
	    let address = items[0].address; 
	    let totalCount = result.totalCount; 
	    let offset = result.offset; 
	    console.log("Item Index: ", offset); 
	    console.log("Items here: ", address); 
	  } ) // End getItems 
	  .catch( (err) => { 
	    let errMsg = err.message; 
	    let errCode = err.code; 
	  } ); //End Catch Error 
	} // End For Loop 

  }); // End onReady() 

} // End Function

Still working on it, but that gives people an idea anyway.

Very useful your code! Thank you!

The code may run more quickly if you do a single call to getItems and then cycle through the resulting array

$w(‘#dealerDataset’).getItems(0, $w(‘#dealerDataset’).getTotalCount())
.then((result) => {
    for (let i= 0 ; i < allItems.count ; i++) {
        ...
    }
});

It’s a single database lookup instead of n+1.

Steve

Thank you Steve! I’ll try it

i used the code that websitejazz suggested and it populates Items as an array. How can I target the data within the array now? I am getting back the following image when I log just (items). How can I get at this info to apply more logic based on results?