Search code examples
node.jsmongodbmongooseschemamongoose-schema

Is there a way to set up mongoose to check if two values match in a single schema?


I'm new to node.js and am required to do some basic validation to check if two fields (in this case, passwords) match and if it doesn't, to display an error message. I'm not worrying about encryption at this point because it's outside of the scope as this is the last part of a uni task.

I've tried creating variables that it can pass on to another but that didn't help much.

Here's a shortened snippet of what I have.

Schema:

const customerSchema = new mongoose.Schema(
    {
        password: 
        {
            type: String, 
            required: true,
        },

        rePassword:
        {
            type: String, 
            required: true,
        },

Posted

app.post("/", function(req,res)
{
    let newCustomer = Customer(
        {
            password: req.body.cPass,
            rePassword: req.body.cConfPass,

Any help is appreciated :)


Solution

  • Solved!

    Turns out I needed to do it just before I ran the function later and it looks like

        if(req.body.cPass != req.body.cConfPass)
        {
            console.log("passwords don't match")
        }
        else
        {
            newCustomer.save();
        }