其他分享
首页 > 其他分享> > 模块的删除

模块的删除

作者:互联网

1.在终端启动app.js

2.创建模块的字段

 

 

//导入mongoose
const mongoose = require("mongoose");
//建立数据库连接
mongoose.connect("mongodb://127.0.0.1:27017/test");

const tagSchema = new mongoose.Schema({
    // text:String,
    text: {
        type: String,
        minlength: 2,
        maxlength: 12
    }
});
const contentSchema = new mongoose.Schema({
    title: {
        type: String,
        minlength: 6,
        maxlength: 12
    },
    content: {
        type: String,
        minlength: 10,
        maxlength: 50
    },
    top: {
        type: Boolean
    }
});
const userSchema = new mongoose.Schema({
    username: {
        type: String,
        minlength: 2,
        maxlength: 12
    },
    password:{
        type: String,
        minlength: 6,
        maxlength: 12
    },
    email:{
        type: String,
        minlength: 6,
        maxlength: 20
    },
    group:{
        type: String,
        minlength: 4,
        maxlength: 12
    }
});
const tagModel = new mongoose.model("tag", tagSchema);
const contentModel = new mongoose.model("content", contentSchema);
const userModel = new mongoose.model("user", userSchema);

 

3.导出成功和失败的函数

 4.

 

    获取参数的方式     get     ctx.query     post    ctx.request.body     delete  ctx.request.body     put修改     ctx.query  ctx.request.body  

 

//导出
module.exports = function (router) {
    router.get("/tag", async (ctx) => {
        try{
            const data=await tagModel.find({});//await 等待
            return success(ctx,data);
        }catch(error){
            return fail(ctx,error)
        }
    })
    router.post("/tag",async ctx=>{
        try{
            const data=await tagModel.create(ctx.request.body);//await 等待
            return success(ctx,data);
        }catch(error){
            return fail(ctx,error)
        }
    })
    router.delete("/tag",async ctx=>{
        try{
            const data=await tagModel.deleteOne(ctx.request.body);//await 等待
            return success(ctx,data);
        }catch(error){
            return fail(ctx,error)
        }
    })
}

删除

 

 5.text.js中,测试页面

 

 

 

 

 

 

 

 

 

标签:const,String,删除,minlength,ctx,模块,mongoose,type
来源: https://www.cnblogs.com/d-hx/p/15625201.html