首页 > 解决方案 > 如何获取日期范围之间的客户列表?

问题描述

如何获取日期之间的客户列表?我的前端反应应用程序中将有一个开始日期和一个结束日期选择器,但我不知道如何在我的快速应用程序中获取特定日期范围内的数据 uisng mongoose。我在下面发布了我的猫鼬模型和路由器代码,将不胜感激 -

猫鼬模型

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const customerSchema = new Schema(
    {
        name: {
            type: String,
            required: true,
            max: 50,
        },
        phone: {
            type: String,
            required: true,
            max: 12,
        },
        address: {
            type: String,
            required: true,
            max: 100,
        },
        pin: {
            type: String,
            required: true,
            max: 6,
        },
        remarks: {
            type: String,
            max: 50,
        },
        isConverted: {
            type: Boolean,
            default: false,
        },
    },
    { timestamps: true }
);

const Customer = mongoose.model(
    'Customer',
    customerSchema
);
module.exports = Customer;

路线

const router = require('express').Router();
const Customer = require('../models/Customer');

router.post('/add', (req, res) => {
    const newCustomer = new Customer({
        name: req.body.name,
        phone: req.body.phone,
        address: req.body.address,
        pin: req.body.pin,
    });
    newCustomer
        .save()
        .then((cx) => {
            if (!cx) {
                res
                    .status(400)
                    .send('Error creating a new customer');
            } else {
                res.send(cx);
            }
        })
        .catch((err) => {
            res.status(500).json({ err });
        });
});

router.get('/list', (req, res) => {
    Customer.find()
        .then((cx) => {
            if (!cx) {
                res
                    .status(400)
                    .send('Error getting customer');
            } else {
                res.send(cx);
            }
        })
        .catch((err) => {
            res.status(500).json({ err });
        });
});

router.get('/getbyphone/:phone', (req, res) => {
    Customer.findOne({ phone: req.params.phone })
        .then((cx) => {
            if (!cx) {
                res
                    .status(400)
                    .send('Error getting customer');
            } else {
                res.send(cx);
            }
        })
        .catch((err) => {
            res.status(500).json({ err });
        });
});

router.get('/getbyname', (req, res) => {
    Customer.findOne({ name: req.body.name })
        .then((cx) => {
            if (!cx) {
                res
                    .status(400)
                    .send('Error getting customer');
            } else {
                res.send(cx);
            }
        })
        .catch((err) => {
            res.status(500).json({ err });
        });
});

router.put('/:id', (req, res) => {
    Customer.findByIdAndUpdate(req.params.id, {
        remarks: req.body.remarks,
        isConverted: req.body.isConverted,
    })
        .then((response) => {
            res.send('Successfully updated');
        })
        .catch((err) => {
            res.status(500).json({ err });
        });
});

router.get('/:id', (req, res) => {
    Customer.findById(req.params.id)
        .then((cx) => {
            res.send(cx);
        })
        .catch((err) => {
            res.status(500).json(err);
        });
});

module.exports = router;

标签: javascriptnode.jsmongodbexpressmongoose

解决方案


像这样添加新路线

router.post('/getByDate', (req, res) => {
    Customer.find({ createAt:{$gt: req.body.min,$lt:req.body.max })
        .then((cx) => {
            if (!cx) {
                res
                    .status(400)
                    .send('Error getting customer');
            } else {
                res.send(cx);
            }
        })
        .catch((err) => {
            res.status(500).json({ err });
        });
});

并从前面的石灰发送数据

{
min:mindate,
max:maxdate
}

推荐阅读