【发布时间】:2017-01-15 18:14:39
【问题描述】:
我想在我的模型上定义一个涉及搜索同一模型的文档的方法,这是我尝试过的:
var mongoose = require('mongoose');
var Author = require('./author.js');
var bookSchema = mongoose.Schema({
author : { type: mongoose.Schema.Types.ObjectId, ref: 'author' },
genre: String,
});
bookSchema.methods.findSimilar = function(callback) {
bookSchema.find({'genre': this.genre}).exec(function doThings(err, doc){
/* ... */
});
};
module.exports = mongoose.model('book', bookSchema, 'book');
但是,我得到了TypeError: bookSchema.find is not a function。
我也试过bookSchema.methods.find(),结果一样。我怎样才能解决这个问题?
谢谢,
编辑:
受此answer 的启发,我也尝试了this.model('Book').find(),但我得到了类似的错误:TypeError: this.model is not a function
【问题讨论】:
-
我相信你需要先定义一个模型来使用函数
find()。 API DOCs reference
标签: javascript node.js mongodb