Node add, delete, modify, check interface? (Interface includes conditional search, data acquisition, paging,)

Build express framework

First, we create an empty folder, then type cmd in the folder navigation and press Enter to open the command line, then enter express, and the creation is successful. After entering, we need to install express and mongoose. Here we note that I am using version 5 of mongoose. Just when downloading npm i mongoose@5

Connect to mongodb

const mongoose=require( 'mongoose"
mongoose.connect('mongodb://127.0.0.1:27017/exercise",(err)=>!  //这里我们是本地链接和本地库名
if(!err){
    
    
console.log("连接成功!);
module.exports=mongoose

Create model

Insert image description here
Create a model folder under the file, which is where we create the model. That db.js is where we connect to the local warehouse. Then we create a library called fence.js, and we create the model we want in it.

const mongoose = require("./db")
const Schema = mongoose.Schema
//车辆
let vehicleSchema = new Schema({
    
    
    name: String,//车辆别名
    drivingLicenseId: {
    
     type: mongoose.Types.ObjectId, ref: 'drivingLicense' },//行驶证Id
    licensePlateNumber: String,//车牌号
    driverName: String,//司机姓名
    driverPhone: String,//司机手机号
    securityName: String,//安全员姓名
    securityPhone: String,//安全员手机号
})

//行驶证表
let drivingLicenseSchema = new Schema({
    
    
    licensePlateNumber: String,//车牌号
    frameNumber: String,//车架号
    getDate: Date,//发证日期
    status: Boolean,//年审状态
    annualReviewDate: Date,//年审日期
    expiryDate: Date,//到期日期
    userId: {
    
     type: mongoose.Types.ObjectId, ref: 'user' },//所属人
})

Then here we note that after we create the model, we still need to expose it for reference when we write the interface.

let vehicleModel = mongoose.model('vehicle', vehicleSchema, 'vehicle')
let drivingLicenseModel = mongoose.model('drivingLicense', drivingLicenseSchema, 'drivingLicense')
module.exports = {
    
     vehicleModel, drivingLicenseModel }

Then we created the model successfully. If we want to add test data before creating the interface, we can use the interface model name + create method to create it.

Write interface

//多条件查询,分页,获取数据接口
// 驾驶证获取数据
router.get('/drivingLicense', async (req, res) => {
    
    
  let pageIndex = req.query.page || 1
  let pageSize = req.query.pageSize || 3
  let searchTxt = req.query.kw
  let list;
  let total = 0
  let totalpage = 0
  let all = await drivingLicenseModel.find()
  if (searchTxt) {
    
    
    let reg = new RegExp(searchTxt, 'i')
    list = await drivingLicenseModel.find({
    
     licensePlateNumber: reg }).populate('userId').skip((pageIndex - 1) * pageSize).limit(pageSize)
    total = await drivingLicenseModel.find({
    
     licensePlateNumber: reg }).count();
    totalpage = Math.ceil(total / pageSize);
  } else {
    
    
    list = await drivingLicenseModel.find().populate('userId').skip((pageIndex - 1) * pageSize).limit(pageSize)
    total = await drivingLicenseModel.count();
    totalpage = Math.ceil(total / pageSize);
  }
  res.send({
    
    
    code: 200,
    data: list,
    total: total,
    all,
    totalpage
  })
});
//添加接口
// 驾驶证添加数据
router.post("/drivingLicenseadd", async (req, res) => {
    
    
  console.log(req.body);
  let body = req.body;
  let data = await drivingLicenseModel.create(body);
  res.send({
    
    
    code: 200,
    msg: '添加成功',
    data
  })
});
//修改接口
// 修改行驶证信息
router.post("/drivingLicenseedit/:id", async (req, res) => {
    
    
  let id = req.params.id
  let body = req.body
  await drivingLicenseModel.updateOne({
    
     _id: id }, {
    
     $set: body });
  let one = await drivingLicenseModel.find({
    
     _id: id })
  res.send({
    
    
    code: 200,
    one
  })
})
//删除接口
// 删除地址信息
router.post('/deleteaddress/:id', async(req, res) => {
    
    
    console.log(req.params);
    let id = req.params.id
    await addressmodul.deleteOne({
    
     _id: id })
    res.send({
    
    
        code: 200,
        msg: '删除成功'
    })
})

Guess you like

Origin blog.csdn.net/qq_53509791/article/details/130922301