linnovate / mean

The MEAN stack uses Mongo, Express, Angular(6) and Node for simple and scalable fullstack js applications
http://mean.io
12.13k stars 3.45k forks source link

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

Open eerjuano opened 10 months ago

eerjuano commented 10 months ago

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

Context: The proyect it is in next.js and I'm using mongodb as database provider. It throws the next error:Algo a ido mal 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);
}

} `