Total noob to coding, am learning though, so far I have managed to get a 3rd party api working! only took 3 days .
Anyway I’m creating a wix website that advertises for old cars, the client will input the vehicles registration number into an input box which will return the vehicles details and store the designated details in a database collection. What I need is to take the weight of said vehicle and give them a price based on it’s weight.
Not sure if this is the right way to go about doing this but so far I have got a
but after that i’m stuck with where to go next, I don’t really want to display the weight I just want to give a price for the weight that’s stored in the “vehicleDetails” database for the vehicle that has just used the api. Would I need to create another database with the variable weights and prices stored? So that it would check if the weight is above or below the stored value and give the correct price?
Hopefully this makes any sense probably not lol, any help much appreciated, many thanks, Andy.
Basically I need to take a field (“weight”) from an entry in a collection, then give a price based on a pre determined value for e.g if the stored weight and prices were 0 - 1000kg then the price is £250, if it was 1001 - 2000kg then the price is £350 etc etc…
So if the car that has just been entered into the collection it will take its weight for e.g it was 1850kg then give the price of £350
It’s not enough info.
Please explain in details what you’re trying to do (I don’t understand on which page you run this query, based on what initial data and where the prices are stored).
ok, im trying to give a price for a car that someone wants to sell, that someone will enter the vehicles registration number into an input box thats on the home page. once they have entered the reg num there is a button underneath called ‘find my car’ on clicking this button it will fetch a 3rd party api which returns and stores the data in a collection called ‘VehicleDetails’ with field names ‘Make’, ‘Model’, ‘Engine Size’, ‘Body Style’ and ‘Weight’.
What I need to do is take the ‘Weight’ from the entry that has just been made and display a price, prices will vary depending on how heavy each different vehicle is, on the homepage is a button called scrap value, once that has been clicked then it will run the code, compare the weight and give the correct price for the weight.
I have made an attempt to write some code which is attached below to handle what i need but although it almost works it doesn’t return the same weight each time is different, it must return the weight for the correct car that has entered their reg num and clicked ‘find my car’.
Prices are not stored anywhere at the moment other than in all the if and else statements, i was wondering whether it would be better to store the different prices to weights in a collection? i dont really know, maybe this is a bit to much to do for a beginner but i suppose you have to start somewhere, any help much appreciated
//SCRAPPRICE
export function buttonScrapV_click(event) {
wixData.query("VehicleDetails")
.distinct("weight")
.then((results) => {
if(results.items.length > 0) {
let items = results.items;
let firstItem = items[0];
let length = results.length;
let query = results.query;
let weight = items[0];
if (weight < 700){
$w('#ScrapOffer').text = "Vehicle too lightweight to give a price online please call us"
}else if (weight > 701){
$w('#ScrapOffer').text = "£300";
} if (weight > 1150){
$w('#ScrapOffer').text = "£350";
}else if (weight > 1385){
$w('#ScrapOffer').text = "£400";
} if (weight > 1500){
$w('#ScrapOffer').text = "£450";
}else if (weight > 2000){
$w('#ScrapOffer').text = "£500";
} if (weight > 2500){
$w('#ScrapOffer').text = "Vehicle is too heavy to give a price online please call us";
}
console.log(results.items[0]);
$w("#ScrapOffer").text = items[0]
} else {
// handle case where no matching items found
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
}
You shouldn’t use the vehicle reg num or record ID or model etc. in your query in order to retrieve the correct record.
Something like:
wixData.query("VehicleDetails")
.eq("regNum"/*use the reg num field as appears in your db collection*/, $w("#regNumInput").value)
.distinct("weight")
.then((results) => {
//etc..
Thank you very much J.D for the help, that works perfectly, had a bit of trouble at first where it would return an undefined result, but realised that the value stored in the collection for reg num was in caps and the reg num input .value was entered in normal not caps, managed to sort it with a .toUpperCase after .value.
I know im probably starting to/ am getting on your nerves now but if you wouldn’t mind i have 1 more thing that i’m having trouble understanding, that is taking the result from the query and giving a price based on the weight, what i have so far only gives £300 or £450 even though the result from the query was 1428 in weight, should be £350. is there a better way of dealing with this than all the if’s and else if’s that you know of? Many thanks.
export function buttonScrapV_click ( event ) {
wixData . query ( “VehicleDetails” )
. eq ( “vrm” , $w ( ‘#inputVRM’ ). value . toUpperCase ())
. distinct ( “weight” )
. then (( results ) => { if ( results.items.length > 0 ) { let items = results.items ; let query = results.query ; let weight = items [ 0 ];
**if** ( weight < 700 ){
$w ( '#ScrapOffer' ). text = "Vehicle too lightweight to give a price online please call us" ;
} **else if** ( weight > 701 ){
$w ( '#ScrapOffer' ). text = "£300" ;
} **if** ( weight < 1150 ){
$w ( '#ScrapOffer' ). text = "£300" ;
} **else if** ( weight < 1151 ){
$w ( '#ScrapOffer' ). text = "£350" ;
} **if** ( weight < 1500 ){
$w ( '#ScrapOffer' ). text = "£350" ;
} **else if** ( weight > 1501 ){
$w ( '#ScrapOffer' ). text = "£400" ;
} **if** ( weight < 2000 ){
$w ( '#ScrapOffer' ). text = "£400" ;
} **else if** ( weight > 2001 ){
$w ( '#ScrapOffer' ). text = "£450" ;
} **if** ( weight < 2500 ){
$w ( '#ScrapOffer' ). text = "£450" ;
} **else if** ( weight > 2500 ){
$w ( '#ScrapOffer' ). text = "Vehicle is too heavy to give a price online please call us" ;
}
} **else** {
// handle case where no matching items found
}
console . log ( results.items );
It’s because the conditional logics is wrong.
It should be:
if (weight < 700){
//to do
} else if(weight >= 700 && weight < 1150){
//to do
} else if(weight >= 1150 && weight < 1151) {
//to do
} //etc..
Or alternatively use the switch method:
let priceTxt = "";
switch(true){
case weight < 700:
priceTxt = "Vehicle too lightweight to give a price online please call us";
break;
case weight >= 700 && weight < 1150:
priceTxt ="£300";
break;
//etc...
case weight > 2500:
priceTxt = "Vehicle is too heavy to give a price online please call us";
break;
}
$w("#ScrapOffer").text = priceTxt;
Wow you are an absolute legend, used the switch method, works flawlessly, thank you so much for taking the time to help me through all that i have learned so much in the past week about computers and code, anyway i really appreciate it, many thanks.
You’re welcome.
And I suggest you’ll store the prices (with model and weight) in a database collection, so you won’t have to change the code every time you change the price (or every time you add a new model/weight). Then when the user fill-in the weight, query the database and use the result to show the relevant price.