Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate with array of fields throws TypeError: Invalid select() argument. Must be a string or object. #1087

Closed
ghost opened this issue Aug 31, 2012 · 3 comments

Comments

@ghost
Copy link

ghost commented Aug 31, 2012

This works well in mongoose@3.0.3:

schema.statics.findGlobal = function(cb) {
  this.find({}).populate('user', 'login').limit(20).exec(cb);
}

But when I try:

schema.statics.findGlobal = function(cb) {
  this.find({}).populate('user', ['login']).limit(20).exec(cb);
}

It fails with:

TypeError: Invalid select() argument. Must be a string or object.
    at Query.select (/Users/rav/MF/instarest/node_modules/mongoose/lib/query.js:1061:11)
    at Function.findOne (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:970:46)
    at Model.populate [as _populate] (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:139:6)
    at next (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:265:14)
    at next (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:228:57)
    at next (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:228:57)
    at next (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:228:57)
    at next (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:228:57)
    at next (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:228:57)
    at next (/Users/rav/MF/instarest/node_modules/mongoose/lib/model.js:228:57)

Is it intended? How can I populate multiple fields?

@skotchio
Copy link

In mongoose 3.0 populate API was changed. http://mongoosejs.com/docs/api.html#query_Query-populate
Now the second argument of populate function is 'Object' or 'String'

Try this:

 schema.statics.findGlobal = function(cb) {
      this.find({}).populate('user', 'login field1 field2 field3').limit(20).exec(cb);
   }

@ghost
Copy link
Author

ghost commented Aug 31, 2012

Awesome, thanks Lary! Would be nice if http://mongoosejs.com/docs/populate.html reflect this change.

@ghost ghost closed this as completed Aug 31, 2012
aheckmann added a commit that referenced this issue Sep 7, 2012
@Djisu
Copy link

Djisu commented Aug 17, 2018

Thanks, that solved my problem

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants