Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

这个路由这么写有什么问题?我想删掉一条记录

我在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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

获取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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...