javascript - Error 'Object has no method xxx ' while using Mongoose - Stack Overflow

I am trying to use Mongoose for connecting MongoDB with Nodejs .I have my code as below. When I am tryi

I am trying to use Mongoose for connecting MongoDB with Nodejs .

I have my code as below. When I am trying to do a GET/POST via api/users I get the

Error


TypeError: Object function model(doc, fields, skipId) {
[08/14 21:28:06 GMT+0800]     if (!(this instanceof model))
[08/14 21:28:06 GMT+0800]       return new model(doc, fields, skipId);
[08/14 21:28:06 GMT+0800]     Model.call(this, doc, fields, skipId);
[08/14 21:28:06 GMT+0800]   } has no method 'list'

Can someone please explain what I am doing wrong ? I had to split my code into different files because I have a lot of functions and I dont want to mess them either my app.js or index/routes.js

app.js

//database connection 
mongoose.connect('....');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
 console.log("Conncection Success !");
});

users_module= require('./custom_modules/users.js');

users_module.init_users();
...
    app.get('/api/users',user.list);  // routing code from expressjs 

/custom_modules/users.js

function init_users() {

    userSchema = mongoose.Schema({
        //id: Number,
        usernamename: String,
        hash: String,
        ...

    });

    userSchema.methods.list = list;

    UserModel = mongoose.model('User', userSchema);

}

function list() {
    return UserModel.find(function (err, users) {
        if (!err) {
            return users;
        } else {
            return console.log(err);

        }
    });
});

exports.init_users = init_users;

routes/user.js

exports.list = function (req, res){

    var users = UserModel.list();  // <---------- This is the error Line 
    return res.send(users);

}

I am trying to use Mongoose for connecting MongoDB with Nodejs .

I have my code as below. When I am trying to do a GET/POST via api/users I get the

Error


TypeError: Object function model(doc, fields, skipId) {
[08/14 21:28:06 GMT+0800]     if (!(this instanceof model))
[08/14 21:28:06 GMT+0800]       return new model(doc, fields, skipId);
[08/14 21:28:06 GMT+0800]     Model.call(this, doc, fields, skipId);
[08/14 21:28:06 GMT+0800]   } has no method 'list'

Can someone please explain what I am doing wrong ? I had to split my code into different files because I have a lot of functions and I dont want to mess them either my app.js or index/routes.js

app.js

//database connection 
mongoose.connect('....');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
 console.log("Conncection Success !");
});

users_module= require('./custom_modules/users.js');

users_module.init_users();
...
    app.get('/api/users',user.list);  // routing code from expressjs 

/custom_modules/users.js

function init_users() {

    userSchema = mongoose.Schema({
        //id: Number,
        usernamename: String,
        hash: String,
        ...

    });

    userSchema.methods.list = list;

    UserModel = mongoose.model('User', userSchema);

}

function list() {
    return UserModel.find(function (err, users) {
        if (!err) {
            return users;
        } else {
            return console.log(err);

        }
    });
});

exports.init_users = init_users;

routes/user.js

exports.list = function (req, res){

    var users = UserModel.list();  // <---------- This is the error Line 
    return res.send(users);

}
Share Improve this question edited Aug 14, 2013 at 14:30 zs2020 54.6k30 gold badges157 silver badges223 bronze badges asked Aug 14, 2013 at 13:32 geeky_monstergeeky_monster 8,82218 gold badges58 silver badges87 bronze badges 5
  • On which line did you see that error? – zs2020 Commented Aug 14, 2013 at 13:52
  • I updated the error on the Question . And also pointed out the Line where the error occurs . The error occurs inside the routes/user.js – geeky_monster Commented Aug 14, 2013 at 14:22
  • UserModel doesn't have a list method. You added it to the instances of UserModel by using methods. You could do UserModel.list = list to add it. – WiredPrairie Commented Aug 14, 2013 at 14:23
  • If I do userSchema.list= list, will that help also ? – geeky_monster Commented Aug 14, 2013 at 14:30
  • Hi @WiredPrairie , Please put that as an answer and I will accept it . You were right . It worked. – geeky_monster Commented Aug 14, 2013 at 15:20
Add a ment  | 

1 Answer 1

Reset to default 5

The methods property of a model object in Mongoose is used to add functions to instances of model objects. So, in your code, the function list is added to an instance of UserModel. If you instead want to have a static like singleton function, then you could add it directly to the UserModel object returned by the call to mongoose.model('UserModel', userModelSchema);:

UserModel.list = list;

Now, you can call it to return the list of all users.

Note that it's still an asynchronous function though. So, you can't just return the results of calling the function, as it will be empty normally. find is async, so your list function also needs to accept a callback that can be called when the list has been returned:

UserModel.list(function(list) {
   res.render(list);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745307921a4621828.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信