【问题标题】:Using mongoose to update a trip使用 mongoose 更新行程
【发布时间】:2015-01-12 22:02:47
【问题描述】:

我一直在关注the docs 如何使用 Node.js、mongoose、express 更新行程,以及几个堆栈溢出问题 herehere

在我的控制器内部,我最初有一个保存,它正在工作,但我需要将它切换到更新。当我运行代码时,没有任何中断,place 如我所料正确记录到控制台,这进一步表明代码正在运行,但由于某种原因,它没有覆盖具有相同 placeIdentifier 的项目.

我做错了什么阻止更新?

我的控制器:

var place = {
    identifier: placeIdentifier,
    name: placeName,
    location: Location,
    travelStart: startDate,
    travelEnd: endDate,
}

Place.findOneAndUpdate(
    {identifier:placeIdentifier}, 
    { $set: place }, 
    {upsert: true},
    function() {
        console.log(place)
        console.log("place saved...")
    }   
);

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    哦,我明白了。或者更确切地说,这个人得到了它:Mongoose: Find, modify, save

    User.findOne({username: oldUsername}, function (err, user) {
        user.username = newUser.username;
        user.password = newUser.password;
        user.rights = newUser.rights;
    
        user.save(function (err) {
            if(err) {
                console.error('ERROR!');
            }
        });
    });
    

    【讨论】:

    • 我只是改变了一些东西(放入“Place”而不是“User”等)。
    猜你喜欢
    • 2012-08-23
    • 2013-04-10
    • 2021-08-06
    • 1970-01-01
    • 2017-03-12
    • 2022-01-09
    • 1970-01-01
    • 2014-06-24
    • 2018-04-18
    相关资源
    最近更新 更多