NuxtN
Nuxt13mo ago
o.m

What's causing the api call error when using composables?

I have this code

    // Fetch users data
    const { data } = await authRegister(form.value);
    console.log(data);

Which doesn't work well when calling register api. I cant create a new user


This is the composable code
import type { Form } from "~/types/auth";
export const authRegister = async (form: Form): Promise<any> => {
    console.log("Form: ", form);
    return await fetch("/api/auth/register", {
        method: "POST",
        body: JSON.stringify(form),
    });
};

This is my template. Notice that the commented code works but the authRegister does not
const handleSubmit = async () => {
    // Check if passwords match
    if (form.value.password !== form.value.confirm_password) {
        error.value = "Passwords do not match";
        return;
    }

    // Fetch users data
    const { data } = await authRegister(form.value);
    console.log(data);

    // Fetch users data
    // const { data } = await useFetch("/api/auth/register", {
    //     method: "POST",
    //     body: JSON.stringify(form.value),
    // });

    // Check if there is an error
    if (data.value && "error" in data.value.body) {
        error.value = data.value.body.error;
        return;
    }
};
Was this page helpful?