Getting a null value for the error.message when trying to DELETE a record

I have a switch case and the GET works without an issue, but the DELETE and PATCH are not deleting or updating the row. You can see what I've got in the code below - I have RLS turned off and can't seem to identify what's stopping the record from being deleted or updated 🤨
export default async function handler(req, res) {
const jokesID = req.query.id;
const method = req.method;

switch (method) {
case "GET":
const { data: getJoke } = await supabase
.from("jokes")
.select("id, joke, user_id")
.eq("id", jokesID)
.single();
if (getJoke) {
res.status(200).json(getJoke);
} else {
res.status(404).json({
error: "Joke not found. Try checking the id or reload the page.",
});
}
break;
case "DELETE":
const { data: delJoke, error: delError } = await supabase
.from("jokes")
.delete()
.eq("id", jokesID);
if (delJoke) {
res.status(200).json(delJoke);
} else {
res.status(404).json({ error: delError.message });
}
break;
case "PATCH":
let { joke, user_id } = req.body;
const { data: patchJoke, error: patchError } = await supabase
.from("jokes")
// On save, the following line changes "joke" to joke and "user_id" to user_id.
// I think this may be the problem (How to stop?)
.update([{ joke: joke, user_id: user_id }])
.eq("id", jokesID);
if (patchJoke) {
res.status(200).json(patchJoke);
}
if (patchError) {
res.status(400).json({ error: patchError });
}
break;
default:
res.status(405).json({ error: `Method ${method} not allowed` }
);
break;
}
}
export default async function handler(req, res) {
const jokesID = req.query.id;
const method = req.method;

switch (method) {
case "GET":
const { data: getJoke } = await supabase
.from("jokes")
.select("id, joke, user_id")
.eq("id", jokesID)
.single();
if (getJoke) {
res.status(200).json(getJoke);
} else {
res.status(404).json({
error: "Joke not found. Try checking the id or reload the page.",
});
}
break;
case "DELETE":
const { data: delJoke, error: delError } = await supabase
.from("jokes")
.delete()
.eq("id", jokesID);
if (delJoke) {
res.status(200).json(delJoke);
} else {
res.status(404).json({ error: delError.message });
}
break;
case "PATCH":
let { joke, user_id } = req.body;
const { data: patchJoke, error: patchError } = await supabase
.from("jokes")
// On save, the following line changes "joke" to joke and "user_id" to user_id.
// I think this may be the problem (How to stop?)
.update([{ joke: joke, user_id: user_id }])
.eq("id", jokesID);
if (patchJoke) {
res.status(200).json(patchJoke);
}
if (patchError) {
res.status(400).json({ error: patchError });
}
break;
default:
res.status(405).json({ error: `Method ${method} not allowed` }
);
break;
}
}
4 Replies
besimon
besimonOP3y ago
Did some further digging and I'm getting null from both the data and the error 🤷🏻‍♂️
garyaustin
garyaustin3y ago
Well delete and update will not return data if you are on v2. Update requires a .select() to return data. That does not explain your data not changing or being deleted in the database if your jokeID is correct and you have RLS off on the table. Any errors in the Postgres logs under database tab in the dashboard?
besimon
besimonOP3y ago
Thanks for that - I ran the DELETE and it returned the following:
{
"file": "/var/log/postgresql/postgresql.csv",
"host": "db-vebyultoqxmrreimgxzy",
"parsed": [
{
"application_name": null,
"backend_type": "not initialized",
"command_tag": null,
"connection_from": "127.0.0.1:49034",
"context": null,
"database_name": null,
"detail": null,
"error_severity": "LOG",
"hint": null,
"internal_query": null,
"internal_query_pos": null,
"leader_pid": null,
"location": null,
"process_id": 33958,
"query": null,
"query_id": 0,
"query_pos": null,
"session_id": "63511ea2.84a6",
"session_line_num": 1,
"session_start_time": "2022-10-20 10:10:42 UTC",
"sql_state_code": "00000",
"timestamp": "2022-10-20 10:10:42.987 UTC",
"transaction_id": 0,
"user_name": null,
"virtual_transaction_id": null
}
],
"parsed_from": null,
"project": "vebyultoqxmrreimgxzy",
"source_type": "file"
}
{
"file": "/var/log/postgresql/postgresql.csv",
"host": "db-vebyultoqxmrreimgxzy",
"parsed": [
{
"application_name": null,
"backend_type": "not initialized",
"command_tag": null,
"connection_from": "127.0.0.1:49034",
"context": null,
"database_name": null,
"detail": null,
"error_severity": "LOG",
"hint": null,
"internal_query": null,
"internal_query_pos": null,
"leader_pid": null,
"location": null,
"process_id": 33958,
"query": null,
"query_id": 0,
"query_pos": null,
"session_id": "63511ea2.84a6",
"session_line_num": 1,
"session_start_time": "2022-10-20 10:10:42 UTC",
"sql_state_code": "00000",
"timestamp": "2022-10-20 10:10:42.987 UTC",
"transaction_id": 0,
"user_name": null,
"virtual_transaction_id": null
}
],
"parsed_from": null,
"project": "vebyultoqxmrreimgxzy",
"source_type": "file"
}
I don't get a postgres log entry for the PATCH call. I have attached a screenshot below from what's returned on the PATCH call
No description
besimon
besimonOP3y ago
Is there anything in particular that I should be looking for?

Did you find this page helpful?