0

I got an error of Account.findOneAndUpdate is not a function using 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.

1 Answer 1

2

findOneAndUpdate is a method on the model, not the schema.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You striped this line module.exports = mongoose.model('accounts', Account); will it effect other part of my app?
It's functionally still there; I just split it into two statements so that the Account model could be used by updateStatus.
I saw somewhere it is one line. Like var Account = module.exports = mongoose.model('account',AccountSchema);
That way works fine as well. Same result either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.