function execQuery(query) {
return function(fn){
query.exec(function (e, res){
(e) return fn(e);
fn(null, res); //想问下,这个fn是做什么的?去掉fn就没法正确返回数据
});
}
}
this.body = yield execQuery(query);
8 回复
/* 定义Schema */ var mongoose = require(‘mongoose’); var Schema = mongoose.Schema; //先定义你自己的Schema var yourSchema = new Schema({ //… }); //定义查询函数listByQuery,名字自己起 yourSchema.statics.listByQuery(queryParams){ var query = this.find(queryParams); return yield query.exec(); }; module.exports = yourSchema;
/* 在controller调用 */ var yourModel= require(‘…/yourSchema’); this.body=yield yourModel.listByQuery(queryParams);
/* 写的没那么全,看懂基本思路即可 */