fmeyjr
fmeyjr
NNuxt
Created by Kyllian on 2/11/2024 in #❓・help
$fetch catches error, but not able to read body
I don’t have back ticks here. So obviously use back ticks 😂
32 replies
NNuxt
Created by Kyllian on 2/11/2024 in #❓・help
$fetch catches error, but not able to read body
Im on my phone but something like: Catch(e) { Throw createError({ status: 400, statusMessage: ‘error msg: ${e}’ })
32 replies
NNuxt
Created by Kyllian on 2/11/2024 in #❓・help
$fetch catches error, but not able to read body
Throw the error with createError
32 replies
NNuxt
Created by Kyllian on 2/11/2024 in #❓・help
$fetch catches error, but not able to read body
Use try and catch
32 replies
NNuxt
Created by fmeyjr on 2/18/2024 in #❓・help
Empty response in pinia store from server API using $fetch
Update: the code is working as intended in edge.. but not chrome 😂 Thanks again for all your input Alex! Much appreciated! 🙇‍♂️
7 replies
NNuxt
Created by fmeyjr on 2/18/2024 in #❓・help
Empty response in pinia store from server API using $fetch
Server code:
export default defineEventHandler(async (event) => {
const client = await serverSupabaseClient<DB>(event);
const user = await serverSupabaseUser(event);

if (!user) {
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
}

const body = await readBody(event);
console.log("post office readBody", body); // works fine

if (!body) {
throw createError({ statusCode: 400, statusMessage: "Bad Request. No body found!" });
}

const { data, error } = await client
.from("office")
.insert([
{
id: randomUUID(),
name: body.name,
street: body.street,
zip: body.zip,
city: body.city,
organization_id: body.organizationId,
maintainer_id: user.id,
},
])
.select()
.single();

console.log("post office", data); // shows the proper object with data

if (error) {
throw createError({ statusCode: 500, statusMessage: `Can't POST office. ${error}` });
}
);

return data;
});
export default defineEventHandler(async (event) => {
const client = await serverSupabaseClient<DB>(event);
const user = await serverSupabaseUser(event);

if (!user) {
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
}

const body = await readBody(event);
console.log("post office readBody", body); // works fine

if (!body) {
throw createError({ statusCode: 400, statusMessage: "Bad Request. No body found!" });
}

const { data, error } = await client
.from("office")
.insert([
{
id: randomUUID(),
name: body.name,
street: body.street,
zip: body.zip,
city: body.city,
organization_id: body.organizationId,
maintainer_id: user.id,
},
])
.select()
.single();

console.log("post office", data); // shows the proper object with data

if (error) {
throw createError({ statusCode: 500, statusMessage: `Can't POST office. ${error}` });
}
);

return data;
});
7 replies
NNuxt
Created by Kyllian on 2/11/2024 in #❓・help
$fetch catches error, but not able to read body
Its absolutely funny how its always the little things. Thanks!
32 replies
NNuxt
Created by Kyllian on 2/11/2024 in #❓・help
$fetch catches error, but not able to read body
I have a similar problem, that the body is undefined on the server. In my store, I have the following function:
const addOffice = async () => {
if (!state.city || !state.name || !state.street || !state.zip) {
return;
}

// TODO put this in util
// The first letter of Name, Street and city need to be uppercase
state.name = state.name.charAt(0).toUpperCase() + state.name.slice(1);
state.street = state.street.charAt(0).toUpperCase() + state.street.slice(1);
state.city = state.city.charAt(0).toUpperCase() + state.city.slice(1);

const reqBody = {
name: state.name,
street: state.street,
city: state.city,
zip: state.zip,
organizationId: organizationID,
}
console.log("Add office: reqBody", reqBody);

try {
const office = await $fetch("/api/offices", {
method: 'post',
body: reqBody,
});
} catch (error) {
toast.add({ title: `Fehler beim Erstellen von Büro.`, color: "red" });
return;
}
...
const addOffice = async () => {
if (!state.city || !state.name || !state.street || !state.zip) {
return;
}

// TODO put this in util
// The first letter of Name, Street and city need to be uppercase
state.name = state.name.charAt(0).toUpperCase() + state.name.slice(1);
state.street = state.street.charAt(0).toUpperCase() + state.street.slice(1);
state.city = state.city.charAt(0).toUpperCase() + state.city.slice(1);

const reqBody = {
name: state.name,
street: state.street,
city: state.city,
zip: state.zip,
organizationId: organizationID,
}
console.log("Add office: reqBody", reqBody);

try {
const office = await $fetch("/api/offices", {
method: 'post',
body: reqBody,
});
} catch (error) {
toast.add({ title: `Fehler beim Erstellen von Büro.`, color: "red" });
return;
}
...
and on my server route server/api/offices/index.post.ts, i have the function:
export default defineEventHandler(async (event) => {
const client = await serverSupabaseClient<DB>(event);
const user = await serverSupabaseUser(event);

// Throw error if no user is found
if (!user) {
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
}

// Get all offices from the request body
const { body } = await readBody(event);
console.log("post office readBody", body);

// Throw error if there is no body
if (!body) {
throw createError({ statusCode: 400, statusMessage: "Bad Request. No body found!" });
}
...
export default defineEventHandler(async (event) => {
const client = await serverSupabaseClient<DB>(event);
const user = await serverSupabaseUser(event);

// Throw error if no user is found
if (!user) {
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
}

// Get all offices from the request body
const { body } = await readBody(event);
console.log("post office readBody", body);

// Throw error if there is no body
if (!body) {
throw createError({ statusCode: 400, statusMessage: "Bad Request. No body found!" });
}
...
However, the body is undefined on the server. What do i miss?
32 replies