首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongo/Mongoose字段值未被更新

Mongo/Mongoose字段值未被更新
EN

Stack Overflow用户
提问于 2018-03-20 12:11:31
回答 2查看 45关注 0票数 0

我的MongoDB收藏有问题。如果我试图替换项目中的字段,它将不会被保存。它正确地记录新值,但DB中的任何内容都没有真正改变。这里怎么了?

代码语言:javascript
复制
exports.update = function (req, res) {
    const { entryid, userid } = req.params;
    let field;
    ['stop', 'description'].forEach(item => req.query[item] ? field = item : -1);

    db.TimeEntry.findById(entryid).then(function (entry) {
        (req.query[field] === undefined) ? entry[field] = 'no value specified' : entry[field] = req.query[field];
        console.log('v:', entry[field]);

        entry.save(function (err) {
            if (err) console.log(err);

            db.TimeEntry.find({ userId: userid })
                .then(foundEntries => res.status(200).json(foundEntries));
        });
    }).catch(err => console.log(err));
}

模式:

代码语言:javascript
复制
const mongoose = require('mongoose');

const TimeEntrySchema = new mongoose.Schema({
    start: {
        type: mongoose.Schema.Types.Mixed,
        default: Date.now,
        required: true
    },
    stop: mongoose.Schema.Types.Mixed,
    description: String,
    project: String,
    billable: Boolean,
    userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    }
}, { timestamps: true })

TimeEntrySchema.pre('remove', function (next) {
    User.findById(this.userId).then(user => {
        user.entries.remove(this.id);
        user.save().then(function (e) {
            next();
        }).catch(err => console.log(err));
    })
});

const TimeEntry = mongoose.model('TimeEntry', TimeEntrySchema);
module.exports = TimeEntry;

编辑:修改这种方式,仍然没有效果。

代码语言:javascript
复制
` entry[field] = (!req.query[field]) ? 'TESTINGTESTING' : req.query[field];
        entry.markModified('description');

        console.log('v:', entry[field]);

        entry.save().then(function (err) {` 

最后编辑:好的,这是最后的解决方案。

代码语言:javascript
复制
exports.update = function (req, res) {
    const { entryid, userid } = req.params;
    let field;
    ['stop', 'description'].forEach(item => req.query[item] ? field = item : -1);

    db.TimeEntry.update({ _id: entryid }, {
        $set: {
            description: req.query[field] || 'no value specified'
        }
    }).then(function () {
        db.TimeEntry.find({ userId: userid })
            .then((foundEntries) => res.status(200).json(foundEntries))
    }).catch(err => console.log(err));
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-20 14:20:35

尝试使用.update

代码语言:javascript
复制
return db.TimeEntry.update({ _id: entryid }, {
    $set: {
        [field]: req.query[field] || 'no value specified'
    }
  }
}).then(() => db.TimeEntry.find({ _id: entryid }))
  .then((resultEntries) => res.status(200).json(foundEntries))
票数 0
EN

Stack Overflow用户

发布于 2018-03-20 12:24:59

你试过这个:entry.save().exec(callbackFN);而不是entry.save(callbackFN);吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49383897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档