'Supabase - RPC function returning null instead of values

I've created a Supabase RPC function in order to process a structured query on my Supabase database and in order to call the RPC function directly with Javascript.

Here's the function:

  begin
    execute distinct on (bets.path)
      bets.path as bet_path,
      bets.quota as quota,
      bets.created_at as created_at,
      bets.created_by as created_by,
      nfts1.username as winner_username,
      nfts1.name as winner_name,
      nfts1.profile_picture_url as winner_profile_picture_url,
      nfts2.username as loser_username,
      nfts2.name as loser_name,
      nfts2.profile_picture_url as loser_profile_picture_url,
      (
          select sum(stakes.crypto_amount)
          from "nfts-floor-price-stakes" stakes
          where bets.path = stakes.bet_path
      ) as crypto_sum
      from
          "nfts-floor-price-bets" bets
          inner join nfts nfts1 on
              bets.winner_username = nfts1.username
          inner join nfts nfts2 on
              bets.loser_username = nfts2.username
          left join "nfts-floor-price-stakes" stakes on
              bets.path = stakes.bet_path;
  end
  $$
language plpgsql;

When I click the "RUN" button on the "Create a RPC function" section of Supeabase, I've got a success message. "Success. No rows returned"

When performing the raw query directly to the database, it gives me the correct results. (n rows with 11 columns)

enter image description here

The query is:

select distinct on (bets.path)
  bets.path as bet_path,
  bets.quota as quota,
  bets.created_at as created_at,
  bets.created_by as created_by,
  nfts1.username as winner_username,
  nfts1.name as winner_name,
  nfts1.profile_picture_url as winner_profile_picture_url,
  nfts2.username as loser_username,
  nfts2.name as loser_name,
  nfts2.profile_picture_url as loser_profile_picture_url,
  (
      select sum(stakes.crypto_amount)
      from "nfts-floor-price-stakes" stakes
      where bets.path = stakes.bet_path
  ) as crypto_sum
  from
      "nfts-floor-price-bets" bets
      inner join nfts nfts1 on
          bets.winner_username = nfts1.username
      inner join nfts nfts2 on
          bets.loser_username = nfts2.username
      left join "nfts-floor-price-stakes" stakes on
          bets.path = stakes.bet_path;

I also tried to change the RPC function, using "perform" instead of execute.

With execute, the response is:

{
"data": [
{
"error": {
"message": "query returned 11 columns",
"code": "42601",
"hint": null,
"details": null
},
"data": null,
"count": null,
"status": 400,
"statusText": "Bad Request",
"body": null
}
]
}

When using perform, it gives me an empty response (status: 200)

My question is: how can I make the function give me the same result of the query?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source