Reputation: 11
I could not understand, but when a User is created, date means the date of account creation. But when a User updates their information, the 'Date' is updating too. What am I doing wrong?
My model:
var userinfoSchema = new mongoose.Schema({
type_ : String,
firstname : String,
surname : String,
organization : String,
name : String,
person_role : String,
members_num : Number,
address : String,
postal_code : String,
city : String,
country : String,
tel : String,
date: { type: Date, default: Date.now }
});
var UserSchema = new mongoose.Schema({
username: String,
password: String,
userinfo: [userinfoSchema]
});
And my app function:
app.put("/Edit/:id", function(req,res) {
User.findByIdAndUpdate(req.params.id, {$set: { userinfo: req.body.userinfo}},
function(err, updated) {
if(err) {
res.redirect('/Edit');
} else {
console.log(updated);
res.redirect('/Profil');
}
});
});
Upvotes: 0
Views: 925
Reputation: 11
This is the best answer i think, after searching for one week...
As of Mongoose 4.0 you can now set a timestamps option on the Schema to have Mongoose handle this for us:
var thingSchema = new Schema({..}, { timestamps: true });
So in this case : the model should be like this :
var userinfoSchema = new mongoose.Schema({
type_ : String,
firstname : String,
surname : String,
organization : String,
name : String,
person_role : String,
members_num : Number,
address : String,
postal_code : String,
city : String,
country : String,
tel : String,
});
var UserSchema = new mongoose.Schema({
username: String,
password: String,
userinfo: [userinfoSchema]
}, { timestamps: true });
Like, we could see : date: { type: Date, default: Date.now } is not a good option to do this.
Timestamps create two fields : "createdAt" & "updatedAt" like we can see in the image below : createdAt will never change and updatedAt will change evertytime the user will update their datas
NB : Also we could change "createdAt" by "date" or howether like this :
var UserSchema = new mongoose.Schema({
username: String,
password: String,
userinfo: [userinfoSchema]
}, { createdAt: 'date', updatedAt: 'update_date' });
Upvotes: 1
Reputation: 1867
There are 2 ways you can keep the date
the same. The first and simplest ways is to sanitize the data from the client:
app.put("/Edit/:id", (req,res) => {
delete req.body.userinfo.date;
User.findByIdAndUpdate(req.params.id, {$set: { userinfo: req.body.userinfo}}, (err, updated) => {
// ...
});
});
You should probably sanitize and verify the data anyways before saving it directly to the database, anyways.
Alternatively, you could first query for the document that needs to be updated and use the save()
. Your schema would look something like this:
var userinfoSchema = new mongoose.Schema({
type_ : String,
firstname : String,
surname : String,
organization : String,
name : String,
person_role : String,
members_num : Number,
address : String,
postal_code : String,
city : String,
country : String,
tel : String,
date : {
type : Date,
default : Date.now,
set : (date) => {
if(this.date) {
return;
}
this.date = date;
}
}
});
To update the User, though, you would need to do something along these lines:
const _ = require('lodash');
app.put("/Edit/:id", (req,res) => {
delete req.body.userinfo.date;
User.findById(req.params.id, (err, user) => {
if(err) {
// Handle error.
}
_.assign(user, req.body.userinfo);
user.save().then(/*...*/);
});
});
Upvotes: 1