EJS 中的数组在 NodeJS 中返回未定义和空值 - Express
Arrays in EJS returning undefined and null value in NodeJS - Express
这是我的ejs代码
<% var tag = [''] %>
<form action="/like" method="POST">
<% for (var i in tag){%>
Tag: <input type="text" name="likes[]" value="<%= tag[i].likes %>"/><br><br>
<button type="submit" value="accept">Send Tag</button><br><br><hr>
<%} %>
</form>
这是我的controller.js代码
control.post('/like', (req, res, next) => {
let tags = [req.body.likes];
console.log(tags);
iglike(tags);
next();
res.send(tags);
});
当我从 name = "likes[]"
中删除 []
时,代码 returns 完美地输入了数据。
但是 name = "likes[]"
控制台日志中的代码 returns undefined
。
例如:
有喜欢[]
Input : App, Web
Output: [null] , In Console: Undefined
没有点赞[]
Input: app, web
Output: ['app, web'] ( Stores them into single array)
我希望我的输出是
Input: app, example, thanks
Output: ['app','example','thanks']
我正在使用 EJS 视图引擎和 node-express
我也在我的 app.js 中添加了 app.use(express.urlencoded({extended: false}))
代码。
更改扩展为真:
app.use(express.urlencoded({extended: true}))
并从 req.body.likes 中删除方括号:
let tags = req.body.likes;
这是我的ejs代码
<% var tag = [''] %>
<form action="/like" method="POST">
<% for (var i in tag){%>
Tag: <input type="text" name="likes[]" value="<%= tag[i].likes %>"/><br><br>
<button type="submit" value="accept">Send Tag</button><br><br><hr>
<%} %>
</form>
这是我的controller.js代码
control.post('/like', (req, res, next) => {
let tags = [req.body.likes];
console.log(tags);
iglike(tags);
next();
res.send(tags);
});
当我从 name = "likes[]"
中删除 []
时,代码 returns 完美地输入了数据。
但是 name = "likes[]"
控制台日志中的代码 returns undefined
。
例如: 有喜欢[]
Input : App, Web
Output: [null] , In Console: Undefined
没有点赞[]
Input: app, web
Output: ['app, web'] ( Stores them into single array)
我希望我的输出是
Input: app, example, thanks
Output: ['app','example','thanks']
我正在使用 EJS 视图引擎和 node-express
我也在我的 app.js 中添加了 app.use(express.urlencoded({extended: false}))
代码。
更改扩展为真:
app.use(express.urlencoded({extended: true}))
并从 req.body.likes 中删除方括号:
let tags = req.body.likes;