fmeyjr
fmeyjr
NNuxt
Created by fmeyjr on 2/18/2024 in #❓・help
Empty response in pinia store from server API using $fetch
On a server route server/api/offices/index.post.ts, supabase is (successfully) used to update an "office". We are returning this office on the server route. In the office pinia store, we use this method to add the office via the server:
const addOffice = async () => {
const reqBody = {
name: state.name,
street: state.street,
city: state.city,
zip: state.zip,
organizationId: organizationID,
};

try {
const data = await $fetch("/api/offices", {
method: "post",
body: reqBody,
});
offices.value.push(data);
} catch (error) {
toast.add({ title: `Fehler beim Erstellen vom Standort. [Error: ${error}]`, color: "red" });
return;
}

state.name = undefined;
state.street = undefined;
state.city = undefined;
state.zip = undefined;
};
const addOffice = async () => {
const reqBody = {
name: state.name,
street: state.street,
city: state.city,
zip: state.zip,
organizationId: organizationID,
};

try {
const data = await $fetch("/api/offices", {
method: "post",
body: reqBody,
});
offices.value.push(data);
} catch (error) {
toast.add({ title: `Fehler beim Erstellen vom Standort. [Error: ${error}]`, color: "red" });
return;
}

state.name = undefined;
state.street = undefined;
state.city = undefined;
state.zip = undefined;
};
However, the data does not contain the added office and is just an empty object. The fetching of offices with $fetch and callOnce does work as expected. We have applied the suggestions from this awesome video: https://www.youtube.com/watch?v=njsGVmcWviY What are we doing wrong? If anyone has an example with pinia + CRUD server API, it would be awesome to share it with us 🙏
7 replies