node.js - 我的猫鼬查询不只返回数据库中的不同数据
问题描述
因为我只想获取他们的数据,他们只有不同的用户名和数据需要最后创建,因为我已经尝试了某些代码,并且我已经分享了这些代码的片段,请帮助我达到预期的输出:)
以前的代码
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));
解决方案
我在很多尝试之后提出了这个解决方案
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));
})
推荐阅读
- vue.js - 尝试在 Vue 中使用 v-for 和 v-if
- python - 尝试使用 python 的 StreamingHttpResponse 创建和下载包含大型数据集的 csv 文件时,如何防止出现 500 CORS 错误?
- javascript - 如何使用正则表达式在字符串的倒数第二个索引中插入特定字符
- reporting-services - 使用 Visual Studio 2019 进行 ssrs 数据集分组的问题
- sql-server - 更改后通过证书为存储过程添加签名
- scala - 在 Spark Dataframe 中的窗口上创建唯一的组 ID
- css - Vue 样式组件
- css - 使用 Material-UI 在卡片中并排放置物品
- python - 深度学习模型训练非常慢的 Jetson Nano
- python - 使用 django 配置用户权限