简体   繁体   中英

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" : "abdullah@gmail.com",
    "password" : "pass1234",
    "passwordConfirm" : "pass1234",
    "passwordChangedAt" : "12-4-2021"
}

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

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.

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

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

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.

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

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.stack.imgur.com/dp5JP.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM