Node.js 在查找猫鼬钩子后修改结果
我试图找到一种方法来修改mongoose的查询结果 下面是带有立柱挂钩的独立模型Node.js 在查找猫鼬钩子后修改结果,node.js,mongodb,mongoose,mongoose-populate,Node.js,Mongodb,Mongoose,Mongoose Populate,我试图找到一种方法来修改mongoose的查询结果 下面是带有立柱挂钩的独立模型 'use strict'; // load the things we need var mongoose = require('mongoose'); var invoice_db = mongoose.createConnection(config.mongo.url + '/invoiceDB'); //connect to buyer DB var path = require('path'); //
'use strict';
// load the things we need
var mongoose = require('mongoose');
var invoice_db = mongoose.createConnection(config.mongo.url + '/invoiceDB'); //connect to buyer DB
var path = require('path');
// define the schema for our invoice details model
var invoicedetailSchema = new Schema({
//SCHEMA INFO
});
invoicedetailSchema.post('find', function(results){
console.log('POST FIRED')
results = results.filter(function(doc){
return doc.tags.length;
})
})
var InvoiceModel = invoice_db.model('InvoiceDetail', invoicedetailSchema);
// create the model for seller and expose it to our app
promise.promisifyAll(InvoiceModel);
promise.promisifyAll(InvoiceModel.prototype);
module.exports = InvoiceModel;
find查询工作正常,post正在启动,但结果不会按照post钩子进行过滤
在返回结果之前,如何编辑结果。过滤器函数不修改调用它的数组,而是基于过滤返回一个新数组。但是为什么不在查询中排除带有空
标记的文档呢?请参阅@johnyhk Ok,可能是过滤机制错误。我要做的就是做到这一点。在pre-hook中,我将对mongoose应用一个填充查询。但是,有些文档对填充没有任何值。我想过滤掉没有填充字段的任何内容。希望这能澄清一点