- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 node.js ejs 和 mongoose 在 for 循环内获取 .length 时遇到问题。我想循环 mongodb 中的一些对象,然后循环每个对象中的数组。我还跳过数据库中的第一个对象,因为这是一个模板对象。我在下面概述了这个问题。 (为了清楚起见,我已从代码中删除了 <% %> Mongoose 标签)
数据库名称=exe
对象在 mongodb 中看起来像这样:
Exe.create({
user: "Marc",
exercise: ["Exercise", "Bench Press"],
previous: [0, 0],
});
正如测试数据一样,有 3 个相同的对象。用户始终只是一个名称,练习和上一个(体重)是数组。我的代码如下所示。
exes.forEach(function(exercise, index){ // irritate over each object
if (index < 1){ // Ignore first object in DB
return;
}else{
exercise.user // Print username from object
for (var i = 0; i = exercise.exercise.length; i++){
exercise.exercise[i] // print [i] item from exercise array
}
}
});
这就是我的代码的样子,但页面无法加载,超时后我在控制台中收到此错误。
<--- Last few GCs --->
114564 ms: Scavenge 823.4 (839.2) -> 815.5 (839.2) MB, 0.1 / 0.0 ms [allocation failure].
114611 ms: Scavenge 823.4 (839.2) -> 815.5 (839.2) MB, 0.1 / 0.0 ms [allocation failure].
114657 ms: Scavenge 823.4 (839.2) -> 815.5 (839.2) MB, 0.1 / 0.0 ms [allocation failure].
114704 ms: Scavenge 823.4 (839.2) -> 815.5 (839.2) MB, 0.1 / 0.0 ms [allocation failure].
114755 ms: Scavenge 823.4 (839.2) -> 815.5 (839.2) MB, 0.1 / 0.0 ms [allocation failure].
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x5302113fa99 <JS Object>
2: /* anonymous */ [0x53021104241 <undefined>:~22] [pc=0x33b7f9be87c] (this=0x12055783d541 <JS Global Object>,exercise=0x3b15653927a9 <a model with map 0x260077d5c781>,index=1)
3: arguments adaptor frame: 3->2
4: InnerArrayForEach(aka InnerArrayForEach) [native array.js:942] [pc=0x33b7f74fdb0] (this=0x53021104241 <undefined>,bq=0x3b1565392921 <JS Function (SharedFunctionInfo 0x1a9413...
FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
1: node::Abort() [node]
2: 0x109cafc [node]
3: v8::Utils::ReportApiFailure(char const*, char const*) [node]
4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [node]
5: v8::internal::Heap::AllocateUninitializedFixedArray(int) [node]
6: v8::internal::Factory::NewUninitializedFixedArray(int) [node]
7: 0xc4bc93 [node]
8: 0x9eb29d [node]
9: v8::internal::Runtime_ArrayPush(int, v8::internal::Object**, v8::internal::Isolate*) [node]
10: 0x33b7f7062bf
Aborted
但是,如果我删除 for 循环并只打印出exercise.length,如下所示:
exes.forEach(function(exercise, index){ // irritate over each object
if (index < 1){ // Ignore first object in DB
return;
}else{
exercise.user // Print username from object
exercise.exercise.length // print how many objects are in the array
}
});
我实际上得到了每个对象返回的值 2。那么为什么它不在 for 循环中指定 i = 数组长度呢?任何帮助将非常感激。
提前致谢马克
最佳答案
for (var i = 0; i = exercise.exercise.length; i++){
应该是:
for (var i = 0; i < exercise.exercise.length; i++){
您当前的代码正在一遍又一遍地将循环索引重置为exercise.length。这就是创建无限循环的原因。循环中间的线应指示继续或停止循环的条件。在这种情况下,只有当 i 小于 (<) 数组大小时,循环才应继续,因为数组索引从 0 到数组长度-1。
关于javascript - for 循环内的 value.length 不起作用。 Mongoose ejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49184762/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!