[英]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[]"
中刪除[]
時,代碼會完美地返回輸入的數據。
但是在name = "likes[]"
上,代碼在控制台日志中返回undefined
。
例如:喜歡 []
Input : App, Web
Output: [null] , In Console: Undefined
沒有喜歡[]
Input: app, web
Output: ['app, web'] ( Stores them into single array)
我希望我的 output 成為
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.