/m1/edit.js

  1. const MembersModel = require("../../models/Members");
  2. const router = require("koa-router")();
  3. router.get("/m1/edit",async ctx=>{
  4. var {id} = ctx.query
  5. var data = await MembersModel.findOne({_id:id})
  6. await ctx.render('edit',{data:data})
  7. })
  8. module.exports = router;

index.html

  1. <a href="/m1/edit?id={{@ $value._id}}" type="button" class="btn btn-warning" >修改</a>

m1/doEdit.js

  1. const router = require("koa-router")();
  2. const path = require("path")
  3. const fileUpload = require("../utils/upload");
  4. const MembersModel = require("../models/Members")
  5. router.post("/m1/doEdit",async ctx=>{
  6. var file = ctx.request.files.file
  7. var uploadName =path.basename(file.path)
  8. var {id,name,age,like,friend} = ctx.request.body
  9. var data={
  10. name,
  11. age: Number(age),
  12. like: Boolean(like),
  13. friend: Boolean(friend)
  14. }
  15. if(file.name.trim()){
  16. /* 将本地图片放到服务器的static文件夹中 */
  17. fileUpload(file.path,uploadName)
  18. var avatar = ctx.origin+"/"+uploadName
  19. data.avatar = avatar
  20. }
  21. await MembersModel.updateOne({_id:id},data)
  22. await ctx.redirect('/m1')
  23. })
  24. module.exports = router;