node.js mongoose无法更新某些字段

时间:2016-08-09 19:34:06

标签: node.js mongoose

我正在尝试更新一个mongodb集合而没有成功的某些值,它更新一个字段而不是另一个字段, 这是我的代码,
架构:

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}
});

用于更新集合的代码

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.'
                                        });
                                    }
                                });
                        }
                    }
                });

这是控制台结果,包括mongoose调试

  

Mongoose:clients.findOne({_ id:ObjectId(" 57aa0403b8f3786d09d8c626")}){fields:undefined}

  之前的数据:{datePayment:2016-08-09T18:03:07.501Z,
    experationPayment:2016-09-09T16:37:34.654Z,
    计划:' starter',
    __v:0,
    密码:' a',
    emailaddress:' a',
    姓氏:' a',
    名字:' a',
    _id:57aa0403b8f3786d09d8c626}

  数据后:{datePayment:2016-08-09T18:46:10.131Z,
    experationPayment:2017-09-09T16:37:34.654Z,
    计划:' starter',
    __v:0,
    密码:' a',
    emailaddress:' a',
    姓氏:' a',
    名字:' a',
    _id:57aa0403b8f3786d09d8c626}

  Mongoose:clients.update({_ id:ObjectId(" 57aa0403b8f3786d09d8c626")}){' $ set':{datePayment:new Date(" Tue,09 Aug 2016 18: 46:10 GMT")}}

  计划成功更新

  Mongoose:clients.findOne({_ id:ObjectId(" 57aa0403b8f3786d09d8c626")}){fields:undefined}

1 个答案:

答案 0 :(得分:0)

我发现了问题,var expTime = data.experationPayment; expTime是一个字符串对象,而不是日期对象,即代码 应为var expTime = new Date(data.experationPayment);

现在我可以使用expTime.getTime()