我遇到的问题是我正确获取项目ID但是当调用delete方法时它只返回404错误。以下是使用的代码: 前端
<% for(var i=0; i<projects.length; i++) {%>
<tr>
<td> <%= projects[i].projectName %> </td>
<td> <%= projects[i].engineer %> </td>
<td> <%= projects[i].finalCost %> </td>
<td> <%= projects[i].status %> </td>
<td>
<button type="button" class="btn btn-outline-warning">Edit</button>
<form method="post" action="/delete/<%=projects[i]._id%>">
<button type="submit" name="delete" class="btn btn-outline-danger">Delete</button>
<input type="hidden" name="_method" value="DELETE" />
</form>
</td>
</tr>
<%}%>
后端:
app.delete('/delete/:_id', function(req, res, next){
console.log(req.params._id);
var project = {_id: req.params._id};
db.collection('projects').remove({_id: project},(err, result) => {
if (err) return console.log(err)
console.log('deleted from database')
res.redirect('/profile')
})
});
答案 0 :(得分:0)
第1步删除ejs文件中的表单并添加<a href="/delete/<%=projects[i]._id%>"<a>
步骤2使用app.get
更改app.delete