Hi,
i’m sorry for my chaotic programming style but i’m kinda new to this.
I got a problem using a function with a returning value.
So the function searches the database for a specific price.
when it finds it it should return it.
when i use “console.log()” into the function itself it gives the right prices
but when i use the function and want to return it to a variable it doesen’t work.
Peace of code:
//getPrice is the function name,
//size wil be another function where a put in a size and it converts it to the needed size
//same goes for size2
function getPrice (dataSet,size,size2) {
let abc;
dataSet.onReady(()=> { //check if the dataset is ready
console.log(size); // make sure the size is correctly converted
//"hoogte" is a name/header in my dataSet
dataSet.setFilter(wixData.filter().eq("hoogte", size)).then(()=> {
dataSet.getItems(0, dataSet.getTotalCount()) // gets all info
.then((result) => {//result is always 1.
Arr = result.items; // Arr is a undifined array "let Arr = [];"
let b = "a"+size2; //the other names/headers from my dataset start with "a"
//size2 is a converted number to match my names/header.
abc = Arr[0][b]; // adds that name/header to my Arr[0] to call it
//"abc" is now a price out of the database
console.log(abc + " - 1")//this returns third "price - 1"
return abc;
})
.catch( (err) => {
let errMsg = err.message;
let errCode = err.code;
console.log("Error getPrice");
} );
console.log(abc + " - 2") // this returns second "Undefined - 2"
//return abc;
});
console.log(abc + " - 3")//this returns first "Undefined - 3"
});
}
when i press a button the code starts running but when i use it in a console.log in the button function
i get this.
this is my code for the button press:
export function btnBestellin_click(event) {
let price = getPrice($w("#datasetkast45pvc42somfyio"), calculateSize($w("#txtBreedte1").value),calculateSize($w("#txtHoogte1").value))
console.log(price);
}
if i add “.toString()” begind the getPrice() function, it returns [object promise] instead of [object object]
I hope someone can help me, been stuck for this for a few days now,