Node.js 无法修改Mongoose文档搜索中的对象
我正在尝试创建一个通用REST,它返回给定模式的所有记录Node.js 无法修改Mongoose文档搜索中的对象,node.js,mongodb,rest,express,mongoose,Node.js,Mongodb,Rest,Express,Mongoose,我正在尝试创建一个通用REST,它返回给定模式的所有记录 /* 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( {} , f
/* 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 );
});
});
对于帐户,我不想返回散列
和盐
,但是o的行为就像它是只读的一样。第二个console.log(o)仍然有salt
和hash
帮助?Mongoose返回不是普通对象的文档实例 因此,您需要首先使用以下方法转换它们: 或者,您可以告诉
find
排除结果中的hash
和salt
字段:
exposed[req.params.id].find({}, '-hash -salt', function(err, docs) { ... });
exposed[req.params.id].find({}, '-hash -salt', function(err, docs) { ... });