node.js - 使用节点 js 和 mongodb 遍历对象数组
问题描述
我是使用 mongodb 的新手,节点正在尝试循环访问我的数据库中的对象数组并仅使用 res.json 显示对象,在我的 console.log 中它显示所有对象,但在我的邮递员中使用 res.json它只显示一个对象请帮助
我的代码
const course = res.GetUserDT.coursesHandled;
for (let index = 0; index < course.length; index++) {
console.log(course[index]);
}
const course = res.GetUserDT.coursesHandled;
for (let index = 0; index < course.length; index++) {
res.json(course[index]);
}
我的控制台输出
{ courseCode: '2103' }
{ courseCode: '2012' }
{ courseCode: '2062' }
我的邮递员输出
{ courseCode: '2103' }
解决方案
您好,欢迎来到 Stackoverflow。
这里的问题是res.json()
向请求者发送一个即时响应 - 这意味着响应已经在您的 for 循环的第一次迭代中发送。
我也想知道为什么你需要那个循环——因为你没有在里面做任何事情。那么你为什么不像这样立即发送数组:
res.json(res.GetUserDT.coursesHandled);
推荐阅读
- python - 在 Python 中实现正则表达式以替换文本文件中每次出现的“meshname = x”
- bert-language-model - 权重和偏差 - 训练 BERT 时如何解释图表
- javascript - javascript文件未链接到单独的html文件
- openslide - 从 OpenSlide openslide_read_region 返回的字节顺序是什么?
- php - 如何在不暴露我的私人凭据的情况下在 WordPress 插件中使用 OAuth 2?
- html - CSS - 即使页面滚动,div 也始终位于屏幕的绝对中间左侧
- java - 如何读取多级xml?
- powershell - 用于卸载软件的 Powershell 脚本
- reactjs - 如何从地图循环中更改状态?
- c++ - 如果邻居具有特定值,OpenCV C++ 获取特定值像素的最快方法