Rumah > Soal Jawab > teks badan
我在roomview.html页面上想要删掉一条显示出来的记录,roomview.html部分代码如下:
<h1>房间列表</h1>
<p>
<a href="/roomview/addroom">增加</a>
</p>
<table>
<thead>
<tr>
<th>_id</th>
<th>roomName</th>
<th>roomContent</th>
<th>roomCreateUser</th>
<th>roomImg</th>
<th>roomCreateDate</th>
<th>roomJoinPeos</th>
<th>roomChat</th>
</tr>
</thead>
<tbody>
<% rooms.forEach(function( room ){ %>
<tr>
<td><%= room._id%></td>
<td><%= room.roomName %></td>
<td><%= room.roomContent %></td>
<td><%= room.roomCreateUser %></td>
<td><%= room.roomImg %></td>
<td><%= room.roomCreateDate %></td>
<td><%= room.roomJoinPeos %></td>
<td><%= room.roomChat %></td>
<td><a href="/roomview/delete?id=<%=room._id%>">删除房间</a> | <a href="/roomview/modifyroom?id=<%=room._id%>">更新房间信息</a></td>
</tr>
<% }); %>
</tbody>
路由roomview.js对应删除部分代码如下:
router.get("/delete/:id",function(req,res){
console.log(req.params.id);
Room.findById(req.params.id,function(err,doc){
if(!doc){
return next(new NotFound("Doc not found"))
}else{
doc.remove(function(){
res.redirect("/roomview");
})
}
});
});
不过当我点击删除房间后页面却显示如下:
Cannot GET /roomview/delete
巴扎黑2017-04-17 16:37:04
获取id
/roomview/delete?id=<%=room._id%>
router.get("/delete", cb)
req.query.id
/roomview/delete/<%=room._id%>
router.get("/delete/:id", cb)
req.params.id
soonfy
怪我咯2017-04-17 16:37:04
@soonfy 就是正解。
你在路由里声明了按照path variable
的形式要求参数:/delete/:id
,
但在页面上却又用了query parameter
的形式提供参数: /roomview/delete?id=<%=room._id%>
。
那肯定会出错,最直观的表现就是你遇到错误,即:压根没找到路由/roomview/delete
对应的处理器,因为你的处理器指定的规则是/delete/:id
,而非/delete
。
解决方案也简单,把页面里改成/roomview/delete/<%=room._id%>
,应该就好了