这是indexloc提供的服务,不要输入任何密码
Skip to content

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

@eerjuano

Description

@eerjuano

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);
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions