简体   繁体   中英

How can i get list of customers between a date range?

How can i get list of customers between a date? There will be a starting date and an ending date picker in my frontend react app, but i dont know how to get the data in a specific date range uisng mongoose in my express app. Im posting my mongoose model and router code below, a little help will be appreciated --

mongoose model

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;

route

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;

add new route like this

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 });
        });
});

and send data from front lime this

{
min:mindate,
max:maxdate
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM