I have a database that has a price field value, and I’m trying to let users filter based on price. When I filter “Under 15,000” it works for values between “10,000 and 15,000”, and when I filter “Under 20,000” it works for values between “10,000, and 20,000”.
But when I filter “Under 10,000” it only shows values between “10,000 and 11,000”. I have queried the inventory using .le, .ge, .between, and it never returns any values under 10,000. I have multiple listings in the database with prices around “5,000”, but it doesn’t return them no matter what query or filter.
My filter code is
$w ( “#dataset1” ). setFilter ( wixData . filter ()
. le ( ‘price’ , 10000 )
);
The value 10,000 is actually carried from another page through session.setitem()
so it inserts the 10,000 from a button click into the .le function. I just need to know how to access values under 10,000.
@russian-dima I read hundreds of forum posts to learn how to code and found you everywhere. I’m now your biggest fan. Any way you could please help me with this?
It sounds very strange to me that all other values except the ones under 10.000 can be found, but 10.000 itself can’t be found!
So let’s do some brainstorming!
What do we have?
- You have a DATABASE.
- You have FIELDS inside your DATABASE
- Your FIELDS are defined into different TYPES (NUMBERS/STRINGS)
Ok, first idea i have when i read your issue and this additional INFORMATION:
The value 10,000 is actually carried from another page through session.setitem()
so it inserts the 10,000 from a button click into the .le function.
Isn’t it STRANGE, that all others do work except → 10.000 <–.
And isn’t → 10.000 ← the only one you insert from somewhere?
So what could be the PROBLEM ?
When you insert your 10.000 …
a) …is it a NUMBER ???
b) …or maybe ist it a STRING ???
TEST IT !!!
console.log(typeof(your VALUE));
I think i know already the answer, because everything what comes out of storage is = STRING !!!
So maybe you need first to convert from STRING → to → NUMBER ???
let myNumber = Number(your STRING here); console.log(typeof (myNumber));
@russian-dima
No no no, the 10,000 itself works fine.
In my comment and in the post, I mention “I just need to know how to access values under 10,000.”
I have queried the database for values like:
wixData.query(‘Inventory’)
.le(‘price’, 5000)
and this returns NOTHING when I console.log(results).
When I query:
wixData.query(‘Inventory’)
.le(‘price’, 10000)
it returns ALL values between $10,000 and $11,000.
When I query:
wixData.query(‘Inventory’)
.le(‘price’, 15000)
it returns ALL values between $10,000 and $15,000.
For some reason I can’t query anything lower than $10,000, unless I explicitly pass
.le(‘price’, ‘exact price’).
How would I pass it as a string if the code to set item is:
$w ( “#button12” ). onClick ( ( event ) => { session . setItem ( ‘price1’ , 10000 ); wixLocation . to ( “/used-cars” )} );
@russian-dima
And would passing it as a string actually help?
Because when I query:
.le(‘price’, 10000)
shouldn’t it show ALL values under $10,000? Not just between $10,000 and $11,000? Everything seems to work above $10,000.
let myString = “10000”;
let myNumber = 10000;
$w("#button12").onClick((event)=>{session.setItem('price1', "10000");wixLocation.to("/used-cars")});
When you are on your → “/used-cars” -page and you will get back the value out of wix-storage, you will get the value back as → STRING as i know.
TESTING your setup!
TEST-DATABASE:
RUNNING-CODE:
import wixData from 'wix-data';
// ------------- USER - INTERFACE -------------------
let DATABASE = 'xxx'
let DBFIELD = 'neuesFeld';
let VALUE = 5000;
// ------------- USER - INTERFACE -------------------
$w.onReady(function () {
wixData.query(DATABASE)
.le(DBFIELD, VALUE)
.find()
.then((res)=>{console.log(res.items)});
});
OUTPUT: FOUND-DATA !
0: {...}
_id:
"0132556e-5bb5-479e-b776-8060f3363eac"
neuesFeld:
1000
1: {...}
_id:
"8b938df3-a1ef-4144-9749-d7ca661fcb90"
neuesFeld:
1500
2: {...}
_id:
"fa45bd9c-202a-4a8d-8599-f6a2891f9275"
neuesFeld:
2500
3: {...}
_id:
"df93fb09-4e58-4d40-b4b3-e0ac5d3ebbce"
neuesFeld:
5000
Data under 5000 found!
I should have mentioned this from the beginning. The values in the price field contain:
10,950 USD.
I ran the following test:
export function checkPrice() {
return wixData.query('Inventory')
.le('price', '20000')
.find()
.then((results) => {
let all = results.items;
all.forEach((record) => {
console.log(record.price)
});
})
.catch((err) => {
let errorMsg = err;
});
}
The output:
The following test:
export function checkPrice() {
return wixData.query('Inventory')
.le('price', '10000')
.find()
.then((results) => {
let all = results.items;
all.forEach((record) => {
console.log(record.price)
});
})
.catch((err) => {
let errorMsg = err;
});
}
The output:
You are using STRINGS for comparisons = BAD IDEA!!!
You must use NUMBERS!!!
Your DB-FIELD must be a NUMBER-FIELD not a STRING-FIELD!
This is also why you get troubles!
NUMBER is not = STRING !
This is what i was talking about just right from the beginning.
Change your DB-FIELD -->price<-- into a NUMBER-FIELD, then everything will work like it should.
The “$” sign can be added by code.
WRONG —> . le ( ‘price’ , ‘10000’ )
RIGHT ------> . le ( ‘price’ , 10000 )
Lol sounds good. The information comes in from an API so I’ll just have to put it on the update schedule using update() and replace(). Was just confused why it returned values in the first place.
Thank you very much for your help!
-Velo-Ninja #1 Fan
To show you a very simple example why it would never work…
This will never be possible → different string-length
5,000 USD —> string-length = 9
10,000USD —> string-length = 10
COMPARISOON POSSIBLE! (same amount of letters+signs → same length)
10,000 USD
12,000 USD
22,000 USD
And this is the answer, why you don’t get anything under 10.000
Now CHANGE all your DATA into NUMBERS and also pay attention when working with wix-Storage!
NUMBER is NOT = STRING ! (always remember!)