1-1 加载页面

在controller 文件夹 新建 doDelete.js

  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const { ObjectId } = require("mongodb")
  4. class DoDeleteController extends Controller {
  5. async index() {
  6. const { ctx } = this;
  7. var { _id } = ctx.query;
  8. var db = this.app.mongodb;
  9. const collection = await db.collection('member');
  10. //找到对应id并删除
  11. var dele = await collection.deleteOne({ _id: ObjectId(_id) });
  12. console.log(dele);
  13. //跳转回home页面
  14. await ctx.redirect("/home")
  15. }
  16. }
  17. module.exports = DoDeleteController;

对应路由

  1. router.get('/detail', controller.detail.index);

通过 home.html 传值

image.png