【问题标题】:node.js mongoose can't update some fieldsnode.js mongoose 无法更新某些字段
【发布时间】:2016-12-15 23:44:26
【问题描述】:

我正在尝试更新一个 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:clients.findOne({ _id: ObjectId("57aa0403b8f3786d09d8c626") }) { fields: undefined }

之前的数据:{ datePayment:2016-08-09T18:03:07.501Z,
exeration付款:2016-09-09T16:37:34.654Z,
计划:“首发”,
__v: 0,
密码:'a',
电子邮件地址:'a',
姓氏:'a',
名字:'a',
_id: 57aa0403b8f3786d09d8c626 }

之后的数据:{ datePayment:2016-08-09T18:46:10.131Z,
exeration付款:2017-09-09T16:37:34.654Z,
计划:“首发”,
__v: 0,
密码:'a',
电子邮件地址:'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 }

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 2021-01-10
      • 2012-04-29
      • 2023-04-09
      • 2015-10-05
      相关资源
      最近更新 更多