gpt4 book ai didi

javascript - for 循环内的 value.length 不起作用。 Mongoose ejs

转载 作者:行者123 更新时间:2023-12-03 02:20:32 61 4
gpt4 key购买 nike

我在使用 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/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com