【问题标题】:How can i get list of customers between a date range?如何获取日期范围之间的客户列表?
【发布时间】:2021-11-04 14:26:15
【问题描述】:

我如何获得一个日期之间的客户列表?我的前端反应应用程序中将有一个开始日期和一个结束日期选择器,但我不知道如何在我的快速应用程序中获取特定日期范围内的数据 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;

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    像这样添加新路线

    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
    }
    

    【讨论】:

    • 你确定它是路由器中的 post req 吗?
    猜你喜欢
    • 2021-01-01
    • 2020-07-06
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多