Enjoying Videos
Enjoying Videos

Reputation: 13

Node Js and mongoose date schema is not working

I have created a mongoose schema and added some schema types

    const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Please tell us your name']
    },
    email: {
        type: String,
        required: true,
        unique: true,
        lowercase: true,
        validate: [validator.isEmail, 'please provide a email']
    },
    photo: String,
    password: {
        type: String,
        required: [true, 'Please provide a password'],
        minlength: 8,
        select: false
    },
    passwordConfirm: {
        type: String,
        required: [true, 'Please Confirm your password'],
        validate: {
            // This only works on CREATE AND SAVE!!!    
            validator: function(el) {
                return el === this.password
            },
            message: 'Passwords are not same'
        }
    },
    passwordChangedAt: Date
});

Then I assign data to the model

{
    "name" : "Abdullah",
    "email" : "[email protected]",
    "password" : "pass1234",
    "passwordConfirm" : "pass1234",
    "passwordChangedAt" : "12-4-2021"
}

Then at the end, "passwordChangedAt" property is not showing in my database

Upvotes: 1

Views: 1536

Answers (6)

Juhi S
Juhi S

Reputation: 1

Check the Type of the "passwordChangedAt" first

userSchema.path('passwordChangedAt') instanceof mongoose.Date; // true

Alternatively, Mongoose schemas support a timestamps option. If you set timestamps: true, Mongoose will add two properties of type Date to your schema:

1. createdAt: a date representing when this document was created
2. updatedAt: a date representing when this document was last updated

Mongoose will then set createdAt when the document is first inserted, and update updatedAt whenever you update the document using save(), updateOne(), updateMany(), findOneAndUpdate(), update(), replaceOne(), or bulkWrite().

Refer to the documentation for more details: https://mongoosejs.com/docs/timestamps.html

Upvotes: 0

Gal Margalit
Gal Margalit

Reputation: 5844

Try setting the type of 'passwordChangedAt' to string instead of date in your schema.
As mentioned above, Mongo can get "picky" about dates and string correlation.

Upvotes: 0

Prosenjit Barman
Prosenjit Barman

Reputation: 81

Make sure that the passwordChangedAt field is included in the related handler to accept the data parsed from the body.

Upvotes: 0

imdshadab
imdshadab

Reputation: 21

As shown in picture.

The real reason for the issue is, passwordChangedAt is not included in the user creation.

Add the code marked below, inside exports.signup, in authController file ( file not shown in your code)

const newUser = await User.create({
    name:req.body.name,
    email:req.body.email,
    password:req.body.password,
    passwordConfirm:req.body.passwordConfirm,
    passwordChangedAt: req.body.passwordChangedAt //add this line
    });

other code continues.....

strong text [1]: https://i.sstatic.net/dp5JP.png

Upvotes: 1

trone
trone

Reputation: 11

In creating a new user, you need to get the requested fields; i.e, name: req.body.name; Like wise, you need to get the requested field of passwordChangedAt: req.body.passwordChangedAt.

Upvotes: 1

Joe
Joe

Reputation: 28316

The value "12-4-2021" is a string, not a Date.

See the description of Date in the MongoDB docs.

Mongoose will attempt to convert the provided value to a Date type. See the Working with Dates tutorial from the Mongoose docs.

Note that every example of a string date in that tutorial uses the "YYYY-MM-DD" format.

Upvotes: 2

Related Questions