/m1/edit.js
const MembersModel = require("../../models/Members");
const router = require("koa-router")();
router.get("/m1/edit",async ctx=>{
var {id} = ctx.query
var data = await MembersModel.findOne({_id:id})
await ctx.render('edit',{data:data})
})
module.exports = router;
index.html
<a href="/m1/edit?id={{@ $value._id}}" type="button" class="btn btn-warning" >修改</a>
m1/doEdit.js
const router = require("koa-router")();
const path = require("path")
const fileUpload = require("../utils/upload");
const MembersModel = require("../models/Members")
router.post("/m1/doEdit",async ctx=>{
var file = ctx.request.files.file
var uploadName =path.basename(file.path)
var {id,name,age,like,friend} = ctx.request.body
var data={
name,
age: Number(age),
like: Boolean(like),
friend: Boolean(friend)
}
if(file.name.trim()){
/* 将本地图片放到服务器的static文件夹中 */
fileUpload(file.path,uploadName)
var avatar = ctx.origin+"/"+uploadName
data.avatar = avatar
}
await MembersModel.updateOne({_id:id},data)
await ctx.redirect('/m1')
})
module.exports = router;