N
Nuxt2y ago
less

useFetch is not defined in server/api

I want to hit the Api using post method in server/api, but whenever i hit it, it return error 500 server error with message "useFetch is not defined"
import { Buffer } from "buffer";

export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
const body = await readBody(event);
const { data } = await useFetch(`${config.public.midtransEndpoint}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization:
"Basic " +
Buffer.from(`${config.public.midtransServerKey}`).toString("base64"),
},
body: {
body,
},
});

return {
data,
};
});
import { Buffer } from "buffer";

export default defineEventHandler(async (event) => {
const config = useRuntimeConfig();
const body = await readBody(event);
const { data } = await useFetch(`${config.public.midtransEndpoint}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization:
"Basic " +
Buffer.from(`${config.public.midtransServerKey}`).toString("base64"),
},
body: {
body,
},
});

return {
data,
};
});
No description
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
warflash
warflash2y ago
☝️ (make sure to check the pinned forum thread)
less
lessOP2y ago
Thankyou for the answer, if may i know, why this issue occured? is it not allowed to useFetch in server/api??
warflash
warflash2y ago
useFetch, useLazyFetch, useAsyncData and useLazyAsyncData only work during setup or Lifecycle Hooks
less
lessOP2y ago
ah i seee, thankyou sir
Want results from more Discord servers?
Add your server