[英]Send Data which I get from EJS to my other NodeJs Script (NodeJs express)
這是我的 Index.js 文件
const ig = require('./like');
const iglike = async() => {
await ig.initialize();
await ig.login('example', 'examplepass');
await ig.liketagsprocess(***tags***);
debugger;
};
module.exports = iglike;
這是我的 controller.js(路線)
control.get('/like', (req, res) => {
res.render("like");
});
control.post('/like', (req, res, next) => {
let tags = req.body.likes;
console.log(tags);
const likeobj = require('./bin/index')(like);
res.send(likeobj.iglike);
next();
res.send(tags);
});
這是我獲取數據的表格 [EJS]
<% (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>
我是一名高中生,使用 Node express 制作我的第一個應用程序 web。 對不起,不好的解釋。
我需要做的是讓表單接受來自用戶的數組並將其發送到“標簽”下的 Index.js。
使用此代碼,我只能接受一個字符串並將其打印在我的控制台中。
當我 go 到 localhost/like
表單顯示並在輸入數據后將其打印在控制台中並在index.js中執行我的代碼
我無法將在控制台中打印的數據傳遞到我的 index.js 文件。
我收到這個錯誤
(node:7140) UnhandledPromiseRejectionWarning: ReferenceError: tags is not defined
謝謝您的幫助!
有幾件事你需要改變:
在你的 index.js 文件中,為你的 function 添加一個參數:
const ig = require('./like');
const iglike = async (tags) => { // < here
await ig.initialize();
await ig.login('example', 'examplepass');
await ig.liketagsprocess(tags);
debugger;
};
module.exports = iglike;
在你的 controller.js 中,你可以在文件頂部導入你的 iglike function 並在你的路由中使用它,將標簽作為參數傳遞:
const iglike = require('./bin/index');
control.get('/like', (req, res) => {
res.render("like");
});
control.post('/like', (req, res, next) => {
let tags = req.body.likes;
console.log(tags);
iglike(tags);
next();
res.send(tags);
});
在您的 ejs 文件中,您應該將輸入的名稱更改為likes[]
以便它可以是一個數組:
<% (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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.