无法 POST 形成 NodeJS Express
Cannot POST form NodeJS Express
我有一个表单,可以将用户的用户数据动态添加到 table。
This is the final form look
当按下其中一个 使用此帐户 时,它显示 Cannot POST /like
。
这是表格。我用EJS
.
<form method="POST" autocomplete="off">
<div class = "form-group col-sm-12">
<label for="tagsarray">Enter Tags</label>
<input type="text" class="form-control" id="likes" name="likes" aria-placeholder="Enter tags seperated by a comma">
</div>
<div class="form-group col-sm-7">
<label for="numberoftimes">Enter Number of Actions</label>
<input type="text" class="form-control" id="action" name="action" aria-placeholder="Enter No. of Actions">
</div>
<% if (accounts) {%>
<table class = "striped">
<tbody>
<% accounts.forEach(accounts => { %>
<tr>
<td><a href="<% accounts._id %>"></a><%= accounts.title%></td>
<td><a href= "/like/<%= accounts._id%>"><button type="submit" class = "btn btn-primary col-auto" >Use this account</button></a></td>
</tr>
</tr>
<%});%>
</tbody>
</table>
<%} else {%>
<p>You have no accounts added</p>
<% } %>
</form>
这是我的controller.js
control.get('/like', async(req, res) => {
try{
const accounts = await account.find({user: req.user.id}).lean()
res.render("backend/like", {
name: req.user.name,
accounts
});
} catch(err) {
console.log(err)
}
});
control.post('/like/:id', getuserdata, (req, res, next) => {
try{
let title = res.accountuser.title;
let pass = res.accountuser.password;
let tags = req.body.likes;
let actions = req.body.action;
console.log(title, pass, tags, actions)
iglike(title, pass, tags, actions)
next();
res.redirect('/like')
}catch(err) {
console.log(err)
}
});
这没有捕获到任何错误,并且控制台完全没有显示任何内容。唯一的错误是 Cannot POST /like
.
这是getuserdata
功能,供参考
async function getuserdata(req, res, next) {
let accountuser
try{
accountuser = await account.findById(req.params.id)
} catch(err) {
console.log(err)
}
res.accountuser = accountuser
next()
};
我试过没有 href
的简单 type=submit
按钮仍然显示相同的错误
请帮我解决这个 Cannot POST /like
错误。
/like 是 GET 请求
对于POST请求你需要使用
/like/1
其中 1 可以是任何参数
您必须为表单设置默认操作,才能使路由与 post 一起使用。如果您使用 href
,它会默认获取。
要在您的路线中创建一个 post,您应该删除 href 并在您的表单中放置一个默认操作。
<form method="POST" autocomplete="off" action="/like/<%= accounts._id%>">
然后在您有提交按钮的行中,删除 href
(因为它已从表单移至默认操作)
<td><button type="submit" class = "btn btn-primary col-auto" >Use this account</button></a></td>
我有一个表单,可以将用户的用户数据动态添加到 table。 This is the final form look
当按下其中一个 使用此帐户 时,它显示 Cannot POST /like
。
这是表格。我用EJS
.
<form method="POST" autocomplete="off">
<div class = "form-group col-sm-12">
<label for="tagsarray">Enter Tags</label>
<input type="text" class="form-control" id="likes" name="likes" aria-placeholder="Enter tags seperated by a comma">
</div>
<div class="form-group col-sm-7">
<label for="numberoftimes">Enter Number of Actions</label>
<input type="text" class="form-control" id="action" name="action" aria-placeholder="Enter No. of Actions">
</div>
<% if (accounts) {%>
<table class = "striped">
<tbody>
<% accounts.forEach(accounts => { %>
<tr>
<td><a href="<% accounts._id %>"></a><%= accounts.title%></td>
<td><a href= "/like/<%= accounts._id%>"><button type="submit" class = "btn btn-primary col-auto" >Use this account</button></a></td>
</tr>
</tr>
<%});%>
</tbody>
</table>
<%} else {%>
<p>You have no accounts added</p>
<% } %>
</form>
这是我的controller.js
control.get('/like', async(req, res) => {
try{
const accounts = await account.find({user: req.user.id}).lean()
res.render("backend/like", {
name: req.user.name,
accounts
});
} catch(err) {
console.log(err)
}
});
control.post('/like/:id', getuserdata, (req, res, next) => {
try{
let title = res.accountuser.title;
let pass = res.accountuser.password;
let tags = req.body.likes;
let actions = req.body.action;
console.log(title, pass, tags, actions)
iglike(title, pass, tags, actions)
next();
res.redirect('/like')
}catch(err) {
console.log(err)
}
});
这没有捕获到任何错误,并且控制台完全没有显示任何内容。唯一的错误是 Cannot POST /like
.
这是getuserdata
功能,供参考
async function getuserdata(req, res, next) {
let accountuser
try{
accountuser = await account.findById(req.params.id)
} catch(err) {
console.log(err)
}
res.accountuser = accountuser
next()
};
我试过没有 href
的简单 type=submit
按钮仍然显示相同的错误
请帮我解决这个 Cannot POST /like
错误。
/like 是 GET 请求
对于POST请求你需要使用 /like/1 其中 1 可以是任何参数
您必须为表单设置默认操作,才能使路由与 post 一起使用。如果您使用 href
,它会默认获取。
要在您的路线中创建一个 post,您应该删除 href 并在您的表单中放置一个默认操作。
<form method="POST" autocomplete="off" action="/like/<%= accounts._id%>">
然后在您有提交按钮的行中,删除 href
(因为它已从表单移至默认操作)
<td><button type="submit" class = "btn btn-primary col-auto" >Use this account</button></a></td>