I'm trying to call an RPC from JS like this: ```ja...
# javascript
k
I'm trying to call an RPC from JS like this:
Copy code
javascript
supabase
  .rpc("getEmptyStats", {
    guild: guild,
    startDates: startDates,
    endDates: endDates,
    dateCount: startDates.length,
  })
  .then((data, error) => {
    console.log(data);
    console.log(error);
  });
The RPC returns an array of ints for some stats queries, however I'm having troubles calling the function and getting this error:
Copy code
bash
{
  error: {
    hint: 'No function matches the given name and argument types. You might need to add explicit type casts.',
    details: null,
    code: '42883',
    message: 'function public.getEmptyStats(dateCount => text, endDates => text, guild => text, startDates => text) does not exist'
  },
  data: null,
  count: null,
  status: 404,
  statusText: 'Not Found',
  body: null
}
Is there a way to typecast this in JS? Or do I need to do it in SQL.
s
You may be able to do
guild: Number(guild)
which might fix it
It seems strange that it's complaining about
startDates => text
though, since
.length
should always return a number in JS
k
I tried doing the
Number(guild)
and it still sees it as text :/
same with the startDates.length
s
Yeah, that's very strange. Do you mind providing an example of the data you're passing in to the function (e.g. what are
guild
,
startDates
,
endDates
defined as)?
k
guild
is an ID and is a string, so I probably should be typecasting it anyways. It looks like
752987588683366500
startDates
and
endDates
are just
[new Date()]
for now while I was testing it.
s
Ah, yeah, so,
new Date()
returns a verbose human friendly date. Adding
.toISOString()
converts it into something that should be understandable to Postgres
Try with that change, and see if it throws any errors (even a different error is progress 🙂 )
k
I'll try quick!
Tried with this code and still the same error 😦
Copy code
javascript
.rpc("getEmptyStats", {
  guild: Number(guild),
  startDates: startDates.map((d) => d.toISOString()),
  endDates: endDates.map((d) => d.toISOString()),
  dateCount: Number(startDates.length),
})
s
I wonder if it's having issues with the camelCasing of the fields. I know postgres is sometimes a little weird with that. If you change it to snake_case (e.g.
start_dates
,
end_dates
,
date_count
) in the stored function as well as in your RPC args, does that work?
k
I can try!
Now it's recognizing the timestamps and dateCount correctly, I think I could do a manual typecast from text to int8 in the function tho. I'm trying that now
Sweet got the typecasting working! Now dealing with my bad SQL 😂 thank you!!!
k
Yeaah, camelcasing is complete bullshit in postgres functions, never do them otherwise u gonna have hard times figuring out what's going on
k
Yep just had another problem with naming a field createdAt and got an error saying "invalid timestamp format" whenever I'd query it
ugh
k
owh yeah.. 😓 been there for few days almost gave up entirely but once u figure this stuff out, u never gonna fell into them again and you will start writing better postgres conventions 🙂
b
PostgreSQL requires you to put anything with a cap in "double quotes" which really stinks. I feel like I'm writing_cobol_from_1985 when I name things in PostgreSQL. That's my only complaint. It's like having a supermodel as a wife who snores. I can deal with that.