Reputation: 1044
i am trying to update a mongodb collection without success on some value, its update one field and not the other ,
here is my code ,
the schema :
var clientSchema = new mongoose.Schema({
emailaddress: {type: String, index: {unique: true}}
, firstname: String
, lastname: String
, domainname: String
, company: String
, password: String
, plan: String
, datePayment: {type: Date, default: Date.now}
, experationPayment: {type: Date, default: Date.now}
});
the code used to update the collection
model.findById(id, function (error, data) {
if (error) {
...
}
else {
if (!data) {
...
}
else {
console.log('data before:', data);
var date = new Date();
var expTime = data.experationPayment;
var comp = new Date();
if (date.getTime() > expTime.getTime()) {
comp = date;
}
else {
comp = expTime;
}
comp.setMonth(comp.getMonth() + month);
data.plan = plan;
data.datePayment = date;
data.experationPayment = comp;
console.log('data after:', data);
data.save(function (error) {
if (!error) {
console.log('plan successfully updated '); res.redirect('/dashboard?message=plan successfully updated');
}
else {
res.json({
message: 'error your plan wasnt saved.'
});
}
});
}
}
});
and here is the console result , with mongoose debug included
Mongoose: clients.findOne({ _id: ObjectId("57aa0403b8f3786d09d8c626") }) { fields: undefined }
data before: { datePayment: 2016-08-09T18:03:07.501Z,
experationPayment: 2016-09-09T16:37:34.654Z,
plan: 'starter',
__v: 0,
password: 'a',
emailaddress: 'a',
lastname: 'a',
firstname: 'a',
_id: 57aa0403b8f3786d09d8c626 }
data after: { datePayment: 2016-08-09T18:46:10.131Z,
experationPayment: 2017-09-09T16:37:34.654Z,
plan: 'starter',
__v: 0,
password: 'a',
emailaddress: 'a',
lastname: 'a',
firstname: 'a',
_id: 57aa0403b8f3786d09d8c626 }
Mongoose: clients.update({ _id: ObjectId("57aa0403b8f3786d09d8c626") }) { '$set': { datePayment: new Date("Tue, 09 Aug 2016 18:46:10 GMT") } }
plan successfully updated
Mongoose: clients.findOne({ _id: ObjectId("57aa0403b8f3786d09d8c626") }) { fields: undefined }
Upvotes: 0
Views: 494
Reputation: 1044
i have found the problem , var expTime = data.experationPayment;
expTime is a string object not a date object, the code
should be var expTime = new Date(data.experationPayment);
and now i could use expTime.getTime()
Upvotes: 0