Supabase RPC调用报错:无法在schema cache找到public.create_room函数
问题描述
我通过Supabase UI创建了一个包含4个参数的PostgreSQL函数,函数定义如下:
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表的INSERT行级策略为:(auth.uid() = room_manager_id),对应角色为authenticated。
我在Next.js API路由中按如下方式调用该函数:
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);
调用后出现如下错误:
{ 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' }
我已确认传入的参数名与函数定义的参数名完全一致。
问题解决
经排查发现,常量rn、rk、sb的值为undefined,导致RPC调用时这些参数被忽略,仅传递了cbi参数,而函数需要4个参数,因此出现找不到对应函数的错误。确保这些常量被赋予实际有效值后,RPC调用成功。
内容的提问来源于stack exchange,提问作者Akash Sharma
相关产品推荐
相关产品推荐

