无法从Mongoose文档搜索中修改对象 [英] Cant modify objects from Mongoose document search
本文介绍了无法从Mongoose文档搜索中修改对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图拥有一个通用的REST,该REST返回给定架构的所有记录.
I am trying to have a generic REST that returns all records for a given schema.
/* Read all entries for a given document type, TODO: limit this to a sensible amount of records, say 500 */
app.get( '/data/all/:id' , verifySession , function( req, res )
{
exposed[req.params.id].find( {} , function(err,docs)
{
if( docs && req.params.id == "Account" )
docs.forEach( function(o){ console.log(o); delete o.salt; delete o.hash; console.log(o); } );
res.json( err || docs );
});
});
对于帐户,我不想返回hash
和salt
,但是o的行为就像是只读的.第二个console.log(o)仍然具有salt
和hash
.
And for Accounts, I dont want to return the hash
and salt
, but o behaves as if it is read only. The second console.log(o) still has salt
and hash
.
帮助?
推荐答案
猫鼬返回不是普通对象的Document实例.
Mongoose returns Document instances, which aren't plain objects.
因此,您需要先使用 toObject
进行转换:>
So you need to convert them first using toObject
:
var documents = docs.map( function(doc) {
doc = doc.toObject();
delete o.salt;
delete o.hash;
return doc;
});
或者,您可以告诉find
在结果中排除hash
和salt
字段:
Alternatively, you can tell find
to exclude hash
and salt
fields in the results:
exposed[req.params.id].find({}, '-hash -salt', function(err, docs) { ... });
这篇关于无法从Mongoose文档搜索中修改对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文