Dataset filter gives always same output independent of filter criteria

Hey everybody!
I am quite new to the platform and also to JavaScript, so please forgive any questions, which might seam obvious.

Here is my problem:
I have a database with different Hotels. Each Hotel has a name (HName), different seasons, different Meal Categories, which can be selected, different rooms etc. In the end I want to filter my DataBase according to the Hotel Name, which was selected in a Dropdown… After having filtered only this Hotel, I want to search for the desired price (according to the season, selected meal and selected room type), which is stored in one of the columns of the database.

Here is my code:
export function DropH1Name_change(event) {
H1Name = $w(’ #DropH1Name ').value;
console.log(‘Name of Hotel 1: ’ + H1Name);
}
function searchHotelPrice (DataSet, HName, HMeal, HRoom, date) {
DataSet.setFilter(wixData.filter()
.contains(“name”, HName));
console.log(DataSet.getCurrentItem().name)
let price = 0;
if (date >= DataSet.getCurrentItem().s1_begin && date <= DataSet.getCurrentItem().s1_end){
switch (HMeal) {
case “BB”:
switch (HRoom) {
case “Single”: price = DataSet.getCurrentItem().s1BbSingle; break;
case “Double”: price = DataSet.getCurrentItem().s1BbDouble; break;
case “Triple”: price = DataSet.getCurrentItem().s1BbTriple; break;
case “Quadruple”: price = DataSet.getCurrentItem().s1BbQuadruple; break;
default: break;}
break;
case “HB”:
switch (HRoom) {
case “Single”: price = DataSet.getCurrentItem().s1HbSingle; break;
case “Double”: price = DataSet.getCurrentItem().s1HbDouble; break;
case “Triple”: price = DataSet.getCurrentItem().s1HbTriple; break;
case “Quadruple”: price = DataSet.getCurrentItem().s1HbQuadruple; break;
default: break;}
break;
case “FB”:
switch (HRoom) {
case “Single”: price = DataSet.getCurrentItem().s1FbSingle; break;
case “Double”: price = DataSet.getCurrentItem().s1FbDouble; break;
case “Triple”: price = DataSet.getCurrentItem().s1FbTriple; break;
case “Quadruple”: price = DataSet.getCurrentItem().s1FbQuadruple; break;
default: break;}
break;
case “AIC”:
switch (HRoom) {
case “Single”: price = DataSet.getCurrentItem().s1AicSingle; break;
case “Double”: price = DataSet.getCurrentItem().s1AicDouble; break;
case “Triple”: price = DataSet.getCurrentItem().s1AicTriple; break;
case “Quadruple”: price = DataSet.getCurrentItem().s1AicQuadruple; break;
default: break;}
break;
default: break;
}
} else if { //much more code, basically the same as above only for other seasons
} else if { price =0;
}
return price;
}
export function button1_click(event) {
let date = $w(’ #DatePickArrival ‘).value;
let cost = 0;
TotHotel1Cost = 0;
for (var i = 0; i < ArrHotel1Rooms.length; i++) {
cost = searchHotelPrice($w(’ #DataHotel ‘), H1Name, H1Meal, ArrHotel1Rooms[i], date);
TotHotel1Cost += cost;
}
$w(’ #text15 ').text = String(TotHotel1Cost);
}

The problem is not getting the value out of a hotel, but first of all to find the hotel. Every time I click the button it gives me the correct cost, but all the time for the same hotel, independent of what H1Name is.
For example: H1Name is “Blue Sapphire Hotel” in Season 1, H1Meal=“FB”, Room Category = “Double” (stored in Array ArrHotel1Rooms). I now, ALWAYS get the cost for e.g. “World Wonders Hotel”, in the correct season 1, with correct meals (full board), room category (double) and therefore correct price, but not for the Hotel I selected.

I have tried various approaches, one time by filtering as above, by setting a query (but this seems to give me an array as an output and no filtered database to which I could apply the code to search in the columns) and also by a “for statement” in which I tried to set the CurrentItemIndex to the dataset which has the corresponding hotel name. But none of those worked! It is actually quite frustrating as I spent hours trying to solve this problem.

Actually, the main thing, which I don´t know is, how to apply the code starting with the if{…} to my result(s) of the filter.

Any help would be greatly appreciated!! Thanks a lot!

You should post the code inside a code block, because it’s hard to read it like that.

and anyway, you forgot to use .then(), and therefore all the code you wrote after the filter, runs before the filtering process has completed.

Hi, thanks for your reply!

Okay, here is the code again in a code block.

export function button1_click(event) {
	let date = $w('#DatePickArrival').value;  
	let pricing;
	TotHotel1Cost = 0;
	for (var i = 0; i < ArrHotel1Rooms.length; i++) {
		pricing = searchHotelPrice($w('#DataHotel'), H1Name, H1Meal, ArrHotel1Rooms[i], date); //
		TotHotel1Cost += pricing;
	}

	$w('#text15').text = String(TotHotel1Cost);
	//console.log(TotHotel1Cost);							
} 
function searchHotelPrice (DataSet, HName, HMeal, HRoom, date) {
    let price = 0;

		DataSet.setFilter(wixData.filter()
			.contains("name", HName)
			.then( (results) => {
			if (date >= DataSet.getCurrentItem().s1_begin && date <= DataSet.getCurrentItem().s1_end){				
			switch (HMeal) {
				case "BB": 
					switch (HRoom) {
						case "Single": price = DataSet.getCurrentItem().s1BbSingle; break;
						case "Double": price = DataSet.getCurrentItem().s1BbDouble; break;
						case "Triple": price = DataSet.getCurrentItem().s1BbTriple; break;
						case "Quadruple": price = DataSet.getCurrentItem().s1BbQuadruple; break;
						default: break;}
					break;
				case "HB":
					switch (HRoom) {
						case "Single": price = DataSet.getCurrentItem().s1HbSingle; break;
						case "Double": price = DataSet.getCurrentItem().s1HbDouble; break;
						case "Triple": price = DataSet.getCurrentItem().s1HbTriple; break;
						case "Quadruple": price = DataSet.getCurrentItem().s1HbQuadruple; break;
						default: break;}
					break;
				case "FB": 
					switch (HRoom) {
						case "Single": price = DataSet.getCurrentItem().s1FbSingle; break;
						case "Double": price = DataSet.getCurrentItem().s1FbDouble; break;
						case "Triple": price = DataSet.getCurrentItem().s1FbTriple; break;
						case "Quadruple": price = DataSet.getCurrentItem().s1FbQuadruple; break;
						default: break;}	
					break;
				case "AIC": 
					switch (HRoom) {
						case "Single": price = DataSet.getCurrentItem().s1AicSingle; break;
						case "Double": price = DataSet.getCurrentItem().s1AicDouble; break;
						case "Triple": price = DataSet.getCurrentItem().s1AicTriple; break;
						case "Quadruple": price = DataSet.getCurrentItem().s1AicQuadruple; break;
						default: break;}
					break;
				default: break;
			}	
		} else {
			price = 0;
		}
		return price;
	}));	
}

I now get the error, “Object does not support property or method .then()”.

What do you advice?
Thanks a lot for any help, really appreciated!

It’s still not formatted well, and it’s hard to read (as it’s not in 1 block),
But I think you’re missing a closing parenthesis ) before the .then

Thanks J.D.! After quite a bit of readjustment it finally filters the correct item and price!

But now a second problem occured. The text object gives “NaN” to me, not a number, which makes sense according to the log.


In the searchHotelPrice function, I give to the console first of all the object, which it filtered {…} and the returned price inside the function. It all works perfectly!

BUT, as you can see, the log gives me first the undefined Room Price, although the statement, which assigns the result of the searchHotelPrice function is written before the console print out of the room price.

I think it takes too much time for the DataSet to apply the filter and all other calculations inside the searchHotelPrice function, so that the print out in the log occurs before there is even a price assigned to roomprice.

Now my question is, how can I delay the assignment of price until the calculations inside searchHotelPrice have finished? Or is there another reason, why I get the undefined and NaN?

Thanks a lot!

You have (at least) 2 mistakes here:

  1. You have to define this onClick() function as an async function and to use await for the searchHotelPrice() function as the latter includes a promise.

  2. In the loop, you override the result of the old cycle, so you’re not going to get the sum-up of all prices.