简体   繁体   English

node.js中的findOneAndUpdate猫鼬失败

[英]findOneAndUpdate mongoose in node.js failed

I got an error of Account.findOneAndUpdate is not a function using POSTMAN. 我收到一个Account.findOneAndUpdate错误不是使用POSTMAN的函数。 Any clue what's wrong with my model below? 任何线索,下面的模型有什么问题?

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');

var Account = new Schema({
    username: String,
    password: String,
    createAt: {type: Date, default: Date.now},
    status: {type: String, default: 'Active'}
});

Account.plugin(passportLocalMongoose);

module.exports = mongoose.model('accounts', Account);

module.exports.updateStatus = function(username,callback){
    var update = {status:'Completed'};
    Account.findOneAndUpdate({username:username},update).exec(callback);
}

I want to update the status to completed 我想将状态更新为已完成

When I do console.log(username) I'm able to see I can get the value. 当我执行console.log(username)时,我可以看到我可以得到该值。

findOneAndUpdate is a method on the model, not the schema. findOneAndUpdate是模型上的方法,而不是模式。

var AccountSchema = new Schema({
    username: String,
    password: String,
    createAt: {type: Date, default: Date.now},
    status: {type: String, default: 'Active'}
});

AccountSchema.plugin(passportLocalMongoose);

var Account = mongoose.model('accounts', AccountSchema);
module.exports = Account;

module.exports.updateStatus = function(username,callback){
    var update = {status:'Completed'};
    Account.findOneAndUpdate({username:username},update).exec(callback);
}

But you probably want to clean up your exports as you're using the model as your exports object but then adding updateStatus to that. 但是您可能想要清理导出,因为您将模型用作exports对象,然后向其中添加了updateStatus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM