【问题标题】:Cant remove field from JSON object - Node JS无法从 JSON 对象中删除字段 - Node JS
【发布时间】:2018-09-28 11:38:47
【问题描述】:

我有一条生成 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字段

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

【问题讨论】:

标签: json node.js mongoose


【解决方案1】:

我在 Feature.find 的末尾用 .select 限制了字段

  .select("-__v -_id");

【讨论】:

猜你喜欢
  • 2018-11-13
  • 2016-06-04
  • 2021-12-18
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
相关资源
最近更新 更多