Troubles doing 'const customerSaved = await newCustomer.save()'

I´m having problems when I try to save information of two different schemas in the same project.

Context: The project is in Next.js, and I’m using MongoDB as the database provider. It throws the following error: “Something has gone wrong. User validation failed: password: The password is required, email: E-mail is required, lastname: Path lastname is required, firstname: Path firstname is required.” PostData: The user.save() works correctly.

`export default async function register(request, response) {
await connectDB();
const { Firstname, Lastname, Email, Password } = request.body;
const stripe = Stripe(process.env.SK_PRIVATE);
console.log(Firstname, Lastname, Email, Password);
try {
    //Registro de Usuario de la pagina en la base de datos
    const UserFound = await User.findOne({ Email });
    if (UserFound)
        return response
            .status(400)
            .json({ message: "El email ya ha sido registrado" });
    if (!Password || Password.length < 7)
        return response
            .status(400)
            .json({ message: "At least needs 7 characters" });
    const hash = await bcrypt.hash(Password, 10);

    const newUser = new User({
        firstname: Firstname,
        lastname: Lastname,
        email: Email,
        password: hash,
    });

    const userSaved = await newUser.save();

    console.log("hecho");
    //Registro de Customer user data en la base de datos
    const customer = await stripe.customers.create({
        name: Firstname,
        email: Email,
    });

    const newCustomer = new Customer({
        userId: userSaved._id,
        stripeCustomerId: customer.id,
    });

    const customerSaved = await newCustomer.save();

    console.log("Cliente guardado con éxito:", customerSaved);

    response.json({
        message: "Se ha registrado correctamente",
    });
    console.log("Datos recibidos en el servidor:", hash, Email);
} catch (error) {
    response.status(500).json({ message: error.message });
    console.log("Algo a ido mal", error.message);
}
}

My theory is that it is not the best way to fill in schemas in one request. Another aspect is that I’ve deleted the logic of the user to see if the other schemas work, and it seems that it recognizes the request as the User Schema. It could be an exportation problem.

Hey @Juan_N_A,

Welcome to the MongoDB Community!

Apologies for the late response.

It seems that the error you are encountering is likely due to the fact that the required fields (firstname, lastname, email, password) in your User schema are not being populated correctly or are missing in your request. This could be the reason that the variables you are trying to destructure from the request body actually do not exist. Please ensure that the request body contains the properties you are expecting.

Also, if you are planning to work with multiple schemas in the same project, make sure to define and handle each schema separately with their respective validation rules and requirements.

I hope this helps!

Best regards,
Kushagra