node.js - MongoDB 数组不显示

标签 node.js mongodb

我想查询我的 MongoDB 数据库,以便仅使用我的 user_id 获取数组中的所有值

我尝试过使用(schema.name),但没有成功

// @route   GET api/user/package
// @desc    Get all package
// @access  Private
router.get('/package', 
  passport.authenticate('jwt', { session: false }),

 (req, res) => {
  Shipments.find({_id : req.user._id},{paymentStatus: "incomplete"})
    .then(shipments => res.status(200).json(shipments.packages))
    .catch(err =>res.status(404).json({ nopackages: 'No package found for you now' }));
});

这是我的预期结果

"packages": [
    {
      "date": "2019-09-23T12:52:14.226Z",
      "_id": "5d88bffe2a6ed7b8d9873548",
      "category": "hazard",
      "quantity": "10",
      "description": "a valueablegoods",
      "length": 10,
      "height": 20,
      "width": 12,
      "weight": 12
    },
    {
      "date": "2019-09-23T12:52:58.129Z",
      "_id": "5d88c02a2a6ed7b8d9873549",
      "category": "hazard",
      "quantity": "10",
      "description": "a valueablegoods",
      "length": 10,
      "height": 20,
      "width": 12,
      "weight": 12
    }
  ],

但是 postman 没有返回任何值,但带来了状态 200,但没有返回任何人

最佳答案

尝试Shipments.findOne()

发生这种情况是因为您正在获取单个用户详细信息。

关于node.js - MongoDB 数组不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58064989/

相关文章:

node.js - 请求正文必须包含以下参数 : 'grant_type' . ..但我把它放在正文中

node.js - 在 Bunyan 日志记录中格式化输出

javascript - Node 加载后停止 vis.js 物理,但允许可拖动 Node

Mongodb 读取首选项

node.js - 如何使用mongodb Node 包在mapReduce中打印

mongodb - Laravel + MongoDB - 检索所有嵌入式模型?

node.js - Mongoose 填充不填充嵌套数组数据

node.js - 无密码和 mongostore

MongoDB聚合查询性能提升

php - 在 php 返回 null 的 mongodb 中聚合查询