首页 > 解决方案 > 我的猫鼬查询不只返回数据库中的不同数据

问题描述

因为我只想获取他们的数据,他们只有不同的用户名和数据需要最后创建,因为我已经尝试了某些代码,并且我已经分享了这些代码的片段,请帮助我达到预期的输出:)

以前的代码

formRouter
  .route("/")
  .get((req, res, next) => {
    Profiles.find(req.query)
      .then(
        (profile) => {
          res.statusCode = 200;
          res.setHeader("Content-Type", "application/json");
          res.json(profile);
        },
        (err) => next(err)
      )
      .catch((err) => next(err));
  })

之前的代码返回

[
    {
        "_id": "5ef4fcaee0a4809078b93d6e",
        "username": "ram",
        "user_tag": "AcDc",
        "old_rank_name": "Recruit"
        "createdAt": "2020-06-25T19:36:14.293Z",
        "updatedAt": "2020-06-25T19:36:14.293Z",
        "__v": 0
    },
    {
        "_id": "5ef747561ad9eb878c176dce",
        "username": "shyam",
        "user_tag": "null",
        "old_rank_name": "Recruit"
        "createdAt": "2020-06-27T13:19:18.009Z",
        "updatedAt": "2020-06-27T13:19:18.009Z",
        "__v": 0
    },
    {
        "_id": "5ef747a31ad9eb878c176dcf",
        "username": "dhyam",
        "user_tag": "null",
        "old_rank_name": "Recruit"
        "createdAt": "2020-06-27T13:20:35.704Z",
        "updatedAt": "2020-06-27T13:20:35.704Z",
        "__v": 0
    },
    {
        "_id": "5ef747ae1ad9eb878c176dd0",
        "username": "dhyam",
        "user_tag": "null",
        "old_rank_name": "Legal Officer"
        "createdAt": "2020-06-27T13:20:46.168Z",
        "updatedAt": "2020-06-27T13:20:46.168Z",
        "__v": 0
    },
    {
        "_id": "5ef747bf1ad9eb878c176dd1",
        "username": "dhyam",
        "user_tag": "null",
        "old_rank_name": "Legal Specialist"
        "createdAt": "2020-06-27T13:21:03.972Z",
        "updatedAt": "2020-06-27T13:21:03.972Z",
        "__v": 0
    }
]

预期输出将类似于具有上次创建数据的不同用户名

[
    {
        "_id": "5ef4fcaee0a4809078b93d6e",
        "username": "ram",
        "user_tag": "AcDc",
        "old_rank_name": "Recruit"
        "createdAt": "2020-06-25T19:36:14.293Z",
        "updatedAt": "2020-06-25T19:36:14.293Z",
        "__v": 0
    },
    {
        "_id": "5ef747561ad9eb878c176dce",
        "username": "shyam",
        "user_tag": "null",
        "old_rank_name": "Recruit"
        "createdAt": "2020-06-27T13:19:18.009Z",
        "updatedAt": "2020-06-27T13:19:18.009Z",
        "__v": 0
    },
    {
        "_id": "5ef747bf1ad9eb878c176dd1",
        "username": "dhyam",
        "user_tag": "null",
        "old_rank_name": "Legal Specialist"
        "createdAt": "2020-06-27T13:21:03.972Z",
        "updatedAt": "2020-06-27T13:21:03.972Z",
        "__v": 0
    }
]

编写我尝试过的代码

global.arr = [];

.get((req, res, next) => {
    Profiles.find().distinct("username")
      .then(
        (profile) => {
          var i;
          for(i=0; i<profile.length; i++)
          {
          Profiles.findOne({ username: profile[i] })
          .sort({ _id: -1 })
          .then(
            (user) => {
              arr.push(user) //from here i tried get array which is global but not working as console.log(arr) works fine
            },
            (err) => next(err)
          )
          .catch((err) => next(err));
        }
          res.json(arr); //as i am not getting updated array here
        },
        (err) => next(err)
      )
      .catch((err) => next(err));

标签: node.jsmongodbmongoose

解决方案


我在很多尝试之后提出了这个解决方案

formRouter
  .route("/")
  .get((req, res, next) => {
    Profiles.find().distinct("username")
      .then(
        (profile) => {
          var i;
          var arr = [];
          for(i=0; i<profile.length; i++)
          {
            var x = i
          Profiles.findOne({ username: profile[i] })
          .sort({ _id: -1 })
          .then(
            (user) => {
              arr.push(user) 

              //added part
              if(arr.length==profile.length)
              {
                res.json(arr);
              }

            },
            (err) => next(err)
          )
          .catch((err) => next(err));
        } 
        },
        (err) => next(err)
      )
      .catch((err) => next(err));
  })

推荐阅读