无法从JSON对象中删除字段 - Node JS

时间:2018-04-18 09:32:08

标签: json node.js mongoose

我有一条生成PDF的路线。

featureRoutes.route('/report/').get(function (req, res) {
Feature.find(function (err,features){
if(err){
 console.log(err);
}
else {
pdf.create(features.toString()).toStream(function (err, stream) {
  if (err) return res.send(err);
  res.type('pdf');
  stream.pipe(res);
});
}
});
});

pdf的内容是一个看起来像这样的JSON对象。

    [ { _id: 5ad5ddddcd054b2b5b20143c,
name: 'Project sidebar',
description: '<p>The project sidebar that we previewed in&nbsp;<a href="https://confluence.atlassian.com/jira/jira-6-4-release-notes-678561444.html">JIRA 6.4</a>&nbsp;is here to stay. We built this new navigation experience to make it easier for you to find what you need in your projects. It&#39;s even better, if you are using JIRA Agile: your backlog, sprints, and reports are now just a click away. If you&#39;ve used the sidebar with JIRA Agile before, you&#39;ll notice that cross-project boards, which include multiple projects, now have a project sidebar as well &mdash;&nbsp;albeit a simpler version.</p>\n',
__v: 0 }

我需要删除_id字段和__V字段。我试着这样做:

    delete features._id

    delete features.__V

但生成的PDF仍然包含id和__V字段

enter image description here

如何在节点中正确删除字段?

1 个答案:

答案 0 :(得分:0)

我在Feature.find

的末尾使用.select限制字段
  .select("-__v -_id");