NodeJS博客实战19_博客内容修改和删除

ゝ一纸荒年。 2022-05-27 08:12 333阅读 0赞

源码地址:https://github.com/RidingACodeToStray/Nodejs_blog

1.主要功能

1)编辑内容页面的渲染

2)编辑内容的分类加载

3)编辑内容的的提交验证

4)内容的删除

2.实现思路

1.使用findOne方法根据传过来的id查找相应的内容并返回

2.使用模板渲染将返回的内容渲染到页面

3.查找分类并渲染到列表上

4.使用populate联表查到分类的id,使用模板渲染判断选中分类

3.相关代码

1)内容修改渲染数据

routers/admin.js

  1. //内容修改
  2. router.get('/content/edit',function(req,res){
  3. var id = req.query.id || '';
  4. var categories = [];
  5. Category.find().sort({_id:1}).then(function(res){
  6. categories = res;
  7. return Content.findOne({
  8. _id:id
  9. }).populate('category');
  10. }).then(function(content){
  11. if(!content){
  12. res.render('admin/error',{
  13. userInfo:userInfo,
  14. message:'指定内容不存在'
  15. });
  16. return Promise.reject();
  17. } else{
  18. res.render('admin/content_edit',{
  19. userInfo:req.userInfo,
  20. categories:categories,
  21. content:content
  22. })
  23. }
  24. })
  25. });

2)内容修改提交保存:

routers/admin.js

  1. //内容修改保存
  2. router.post('/content/edit',function(req,res){
  3. var id = req.query.id || '';
  4. if (req.body.title == '') {
  5. res.render('admin/error',{
  6. userInfo:req.userInfo,
  7. message:'标题不能为空'
  8. })
  9. return;
  10. }
  11. if (req.body.content == '') {
  12. res.render('admin/error',{
  13. userInfo:req.userInfo,
  14. message:'内容不能为空'
  15. })
  16. return;
  17. }
  18. Content.update({
  19. _id:id
  20. },{
  21. category:req.body.category,
  22. title:req.body.title,
  23. desciption:req.body.desciption,
  24. content:req.body.content
  25. }).then(function(){
  26. res.render('admin/success',{
  27. userInfo:req.userInfo,
  28. message:'内容保存成功',
  29. url: '/admin/content/edit?id=' + id
  30. })
  31. })
  32. })

3)内容删除

routers/admin.js

  1. //内容删除
  2. router.get('/content/delete',function(req,res){
  3. var id = req.query.id || '';
  4. Content.remove({
  5. _id:id
  6. }).then(function(){
  7. res.render('admin/success',{
  8. userInfo:req.userInfo,
  9. message:'删除成功',
  10. url:'/admin/content'
  11. });
  12. })
  13. })

4)渲染页面代码

views/admin/content_edit.html

  1. {% extends 'layout.html' %}
  2. {% block main %}
  3. <div class="a-userNav">
  4. <span><a href="/">管理首页</a></span>
  5. <span> / </span>
  6. <span>内容修改 - {
  7. {content.title}}</span>
  8. </div>
  9. <form style="padding-left: 50px;margin-top: 20px;" method="post">
  10. {
  11. {content.category._id.toString()}}
  12. <div>
  13. <label for="category">分 类:</label>
  14. <select name="category" id="category" style="width: 200px;">
  15. {% for category in categories %}
  16. {% if content.category._id.toString() == category._id.toString() %}
  17. <option value="{
  18. {category.id}}" selected>{
  19. {category.name}}</option>
  20. {% else %}
  21. <option value="{
  22. {category.id}}">{
  23. {category.name}}</option>
  24. {% endif %}
  25. {% endfor %}
  26. </select>
  27. </div>
  28. <div>
  29. <label for="title">内容标题:</label>
  30. <input type="text" name="title" id="title" style="width: 200px;" value="{
  31. {content.title}}">
  32. </div>
  33. <div>
  34. <label for="desc">简 介:</label>
  35. <input type="desc" name="desciption" id="desciption" style="width: 200px;" value="{
  36. {content.desciption}}">
  37. </div>
  38. <div>
  39. <label for="content" style="vertical-align: top;">内 容:</label>
  40. <textarea type="content" name="content" id="content" style="width: 200px;" rows="4">{
  41. {content.content}}</textarea>
  42. </div>
  43. <button type="submit">提交</button>
  44. </form>
  45. {% endblock %}

发表评论

表情:
评论列表 (有 0 条评论,333人围观)

还没有评论,来说两句吧...

相关阅读