Reputation: 21
Hi I have created a postgres function using the supabase ui with 4 parameters. see this screenshot
function definition is
begin
insert into public.rooms (created_by_id, room_manager_id, room_name, room_key,starting_balance,room_open,player_id_list)
values (
cbi,
cbi,
rn,
rk,
sb,
true,
ARRAY[] :: uuid[]
);
UPDATE public.players
SET room_id_list = array_append(room_id_list, (SELECT id FROM public.rooms WHERE room_name = rn))
WHERE id = cbi;
end;
public.rooms table has this row level policy check : (auth.uid() = room_manager_id)
with role as authenticated
for INSERT
.
When i am calling this function from a nextjs api route function like this,
const cbi : string= session.user.id
const rn : string= req.body.room_name
const rk : string= req.body.room_key
const sb : number= req.body.starting_balance
let { data, error } = await supabase.rpc("create_room", {cbi, rn, rk, sb} );
if (error) console.error("rpc crete_room error : ",error);
else console.log("rpc create_room success : ",data);
This gives me an error ,
{
code: 'PGRST202',
details: 'Searched for the function public.create_room with parameter cbi or with a single unnamed json/jsonb parameter, but no matches were found in the schema cache.',
hint: null,
message: 'Could not find the function public.create_room(cbi) in the schema cache'
}
i have checked that the parameter names match with the paramater i am passing in the rpc args
UPDATE : Problem SOLVED
I found out that the values in the constants rn rk and sb were undefined
which is why the rpc function was ignoring them. Once I made sure that those constants had actual values rpc was able to identify them as valid arguments and the function call worked like a charm.
Upvotes: 2
Views: 6245
Reputation: 6984
You'll need to specify each parameter in the RPC call i.e:
let { data, error } = await supabase
.rpc("create_room",
{"cbi":cbi, "rn":rn, "rk":rk, "sb":sb }
);
Upvotes: 2