我的目标是在mongoosastic搜索中填充某些字段,但是如果我执行以下代码,它将总是返回

下面是代码
var mongoose = require('mongoose');
var Category = require('./category');
var mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;
var ProductSchema = new Schema({
category: { type: Schema.Types.ObjectId, ref: 'Category', es_schema: Category, es_indexed:true, es_select: 'name'},
name: String,
price: Number,
image: String
});
ProductSchema.plugin(mongoosastic, {
hosts: [
'localhost:9200'
],
populate: [
{path: 'category', select: 'name'}
]
});
module.exports = mongoose.model('Product', ProductSchema);
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CategorySchema = new Schema({
name: { type: String, unique: true, lowercase: true}
});
module.exports = mongoose.model('Category', CategorySchema);api.js
router.post('/search', function(req, res, next) {
console.log(req.body.search_term);
Product.search({
query_string: { query: req.body.search_term }
}, function(err, results) {
if (err) return next(err);
res.json(results);
});
});我应该怎么做才能在mongoosastic中填充某个类别?
发布于 2016-06-09 17:40:08
您正在使用
populate: [
{path: 'categories', select: 'name'}
]但是代码中的categories是undefined。
正如您在模式声明中提到的categories as
,您必须使用category来代替类别
希望它能解决你的问题。
发布于 2017-05-22 22:26:07
编辑:
category: { type: Schema.Types.ObjectId, ref: 'Category', es_schema: Category, es_indexed:true, es_select: 'name'},=> _category : { type: Schema.Types.ObjectId, ref: 'Category' },
并使用populate:.populate('_category ', 'name')
希望能对你有所帮助。
更多信息请访问:http://mongoosejs.com/docs/populate.html
https://stackoverflow.com/questions/35183093
复制相似问题