Calling these methods (gte, lt) on `t` is not poss...
# javascript
n
Calling these methods (gte, lt) on
t
is not possible. (throws error) For better context, what I am trying to do is use my URL params to create a select query. For example,
/inventory?make=Nissan&model=Maxima&year=2018,2021
. I'm currently passing my URL params as an object into
match()
. This works perfectly until I have a range in the params, such as
year=2018,2021
. This will not work. Obviously I need to use the filter methods for the ranges. And this here lies the problem .. or inconvenience. I'm going to have to write multiple IF statements for each possible combination of ranges. (
year
,
price
, &
miles
). Thoughts on possibilities? Thanks!
g
chaining filters
You can chain filters as anothercoder showed in second example. Start with query=supabase.from('cars'); then do an if on year and add the year part like: query=query.gte(year,2018).lt(year,2021);| then another if for model, and a new query=query.nextfilterset; when you are finished you execute the whole query with: const {data} = await query or query.then((data)=>{})
n
I did try this .. let me try it again ...
g
You should only need 1 if per filter type to see if filter is present or not and add or skip that part. You don't have to do a bunch of large query combinations for every type of filter combination.
n
Yeah .. I get TypError
g
I do this by putting all my filters with values in an array and then looping thru it with a case statement to build up the query=query.filterX
n
Uncaught (in promise) TypeError: query.gte is not a function
g
you have to start with query=supabase.from('table')
then keep adding to query
n
let query = supabase.from('vehicles'); query = query.gte('year', 2020);
^ thats what I have
g
are you using typescript?
n
No
g
let query = supabase.from(table) .select('id, type, updated_at, title',{ count: 'exact' }) .eq('uid', user.id) Object.keys(filters).forEach((key) => { let filter = filters[key] console.log('filter',filter.op,filter) switch (filter.op) { case "eq": query = query.eq(filter.col, filter.val.toLowerCase()) break case "contains": query = query.contains(filter.col, [filter.val.toLowerCase()]) break case "like": query = query.ilike(filter.col,
%${filter.val.toLowerCase()}%
) break case "between": query = query.gte(filter.col, filter.val[0]) query = query.lte(filter.col, filter.val[1]) } }) query = query.order(orderObj.column,{ascending:orderObj.ascending}).range(startRecord,startRecord+qLimit-1) let { data: notes, error,count } = await query
that shows running code except for the % thing which is some sort of discord copy/paste issue.
n
Interesting ...
g
the between case is what your are doing
n
You're reassigning query, but it isn't overwriting the
supabase.from(table)
assignment? That's odd just looking at it ...
g
no it just keeps chaining it up
When I'm done it will add up to 10 filters of various types in the loop. Nothing executes until the await or .then
n
Okay
hmm ..
how are you importing
supabase
?
const supabase = createClient('host', 'token')
?
g
import { createClient } from '@supabase/supabase-js' const supabase = createClient()
n
yup ..
thats so strange lol
g
if supabase.from.filter.filter works for you I don't know why the chaining would not work, others use it too as anothercoder also suggested it.
@User OH maybe you have to do the .select first
after the .from. I probably simplified my suggestion to you too much over the real code.
n
Oh ... nice! no type error
okay this should work then
g
Luck
n
Got it working. Thank you @User and @User
a
See "You must apply your filters to the end of your query. For example:" https://supabase.com/docs/reference/javascript/using-filters