Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
eerjuano opened this issue Sep 20, 2023 · 0 comments
Open

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

eerjuano opened this issue Sep 20, 2023 · 0 comments

Comments

@eerjuano
Copy link

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

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant