我正在尝试像这样使用 for 循环。但是我在终端中没有得到“console.log(i)”,而我确实正确地得到了“console.log(req.body)”。请帮忙。
const express = require("express");
const router = new express.Router();
router.post("/basic", async (req, res) => {
try {
res.send(req.body);
console.log(req.body)
for (let i=0; i < 5, i++; ) {
console.log(i);
}
} catch (e) {
res.status(400).send(e);
}
});
module.exports = router;
最佳答案
当您声明变量 let i
时;它的值为 undefined
;所以有2种undefined
在 JavaScript 中:
You may get
undefined
when a variable was not defined (does not exist), and second the variable exists but the value isundefined
the variable exists.
下一个在 for
操作如下i < 5 (undefined < 5)
这是 false
它退出 for 循环,所以这就是为什么你没有得到 console.log
输出。
@Rap Sherlock 建议的最简单的解决方案 let i = 0;
编辑后:
代码中还有一个拼写错误:
for (let i=0; i < 5, i++; ) { // should be
// need to use ; instead of ,
for (let i = 0; i < 5; i++) {
console.log(i);
}
关于javascript - 在 Express Router (node.js) 中使用 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58199120/