Szpok
Szpok

Reputation: 53

object has no find method when searching for distinct '_id'

I'm trying to return all the '_id' values of documents using the following Schema:

var QuestionSchema = new Schema ({
    author: { type: String },
    title: { type: String },
    subject: { type: String },
    topic: { type: String },
    question: { type: String }, 
    correct_ans: { type: Number },
    explanation: { type: String },
    answers: [ String ],
    answer_stats: [ Number ], 
    tags: [ String ],
    publish: { type: Boolean }
});

However, when I run the following code:

var Question = mongoose.model('Question', Question, 'Question');
var question = new Question();
question.find().distinct( '_id' , function(err,questions){
    // Some code
});

I get the following error:

[TypeError: Object { _id: 54cf53aec22cbf242b9cde43,
  tags: [],
  answer_stats: [],
  answers: [] } has no method 'find']

It is only referring to the arrays in the Schema but I'm not searching them. Any thoughts? Thanks in advance.

Upvotes: 0

Views: 74

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51480

.find() is a static method of Mongoose.Model class, but you're calling not existing instance method instead. Here is a correct example:

var Question = mongoose.model('Question', Question, 'Question');
Question.find().distinct( '_id' , function(err,questions){
  // Some code
});

Please, see Mongoose Queries Docs and Mongoose API Docs for more info.

Upvotes: 2

Related Questions