javascript - 如何获取日期范围之间的客户列表?
问题描述
如何获取日期之间的客户列表?我的前端反应应用程序中将有一个开始日期和一个结束日期选择器,但我不知道如何在我的快速应用程序中获取特定日期范围内的数据 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;
解决方案
像这样添加新路线
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
}
推荐阅读
- node.js - 存储 Oauth 访问令牌和刷新令牌
- javascript - javascript 和 css 的 CSP 问题 - 密码可见性切换
- excel - .CurrentPage PivotTable VBA Excel 的运行时错误 1004
- javascript - 链接到数组推送功能
- plotly - 如何在图表上禁用标题版本
- haskell - Haskell 中的序列化
- javascript - 在 mysql 中存储依赖于 javascript 的下拉菜单的值
- arrays - 编译错误 - 数组索引超出数组 blah.groupsToTrace[LENGTH_OF_A_MACRO] = ToTrace[number];
- excel - 不计算的简单公式
- authentication - 如何更新与缓存令牌关联的权限