Model 使用远程方法检查一个日期是否介于两个日期之间
模型如下:Model 使用远程方法检查一个日期是否介于两个日期之间,model,loopback,Model,Loopback,模型如下: car - id(number),brand(String),model(String),color(String) bookingCar - id (number),carId (number),startDate(date),endDate(date),location(string),carType(Sring) 关系: car has many bookingCar (foreign key: carId) bookingCar belongs to car
car
- id(number),brand(String),model(String),color(String)
bookingCar
- id (number),carId (number),startDate(date),endDate(date),location(string),carType(Sring)
关系:
car has many bookingCar (foreign key: carId)
bookingCar belongs to car (foreign key: carId)
现在,我想从汽车
模型中筛选数据,根据该模型,在所选日期段和位置、carType中没有预订
所以我引用了remotemethode,并使用了lb remotemethode
。
基于这个链接,我写了一个方法
'use strict';
module.exports = function(Bookingcar) {
var BookingCar = require('./booking-car.json');
BookingCar.findCar = function(location, startDate, endDate, carType, callback) {
var results;
// TODO
const d1 = startDate.split("-");
const d2 = endDate.split("-");
var c = dateCheck.split("-");
var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);
var to = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);
if((check <= to && check >= from)){
var filter = { include: [
'brand',
'model',
'color',
'carType',
'revenue_li_cpy',
'revenue_li_ex_date',
'ins_cpy',
'ins_cpy',
'ins_exp_date',
'car_photo',
'ownerNICId_companyName'],
fields: [
'id','brand',
'model',
'color',
'carType',
'revenue_li_cpy',
'revenue_li_ex_date',
'ins_cpy',
'ins_cpy',
'ins_exp_date',
'car_photo',
'ownerNICId_companyName'], where: { and: [{ location: location }, { carType: carType },{ startDate: d1 },{ endDate: d2 }, { status: 1 }] } };
}
Bookingcar.find(location, startDate, endDate, carType, {
include: [{
relation: 'car'}
]},
function(err, results) {
if (err)
console.log(err);
else {
callback(null, results);
}
});
};
Bookingcar.remoteMethod(
'findCar', {
http: {path: '/findCar', verb: 'get'},
accepts: [
{arg: 'location', type: 'string'},
{arg: 'startDate', type: 'date'},
{arg: 'endDate', type: 'date'},
{arg: 'carType', type: 'string'}
],
returns: [
{ arg: 'id', type: 'number', description: 'id', required: true, root: true },
{ arg: 'brand', type: 'string', required: true, root: true },
{ arg: 'model', type: 'string', required: false, root: true },
{ arg: 'color', type: 'string', required: true, root: true },
{ arg: 'carType', type: 'string', required: false, root: true },
{ arg: 'regiNo', type: 'string', required: true, root: true },
{ arg: 'revenue_li_cpy', type: 'string', required: false, root: true },
{ arg: 'revenue_li_ex_date', type: 'date', required: true, root: true },
{ arg: 'ins_cpy', type: 'string', required: false, root: true },
{ arg: 'ins_cpy', type: 'string', required: true, root: true },
{ arg: 'ins_exp_date', type: 'date', required: false, root: true },
{ arg: 'car_photo', type: 'string', required: true, root: true },
{ arg: 'ownerNICId_companyName', type: 'string', required: false, root: true },
]
}
);
};
“严格使用”;
module.exports=功能(预订车){
var BookingCar=require('./BookingCar.json');
BookingCar.findCar=函数(位置、开始日期、结束日期、类型、回调){
var结果;
//待办事项
常数d1=起始日期拆分(“-”);
常数d2=结束日期分割(“-”);
var c=日期检查。拆分(“-”);
var from=新日期(d1[2],parseInt(d1[1])-1,d1[0]);
var to=新日期(d2[2],parseInt(d2[1])-1,d2[0]);
var检查=新日期(c[2],parseInt(c[1])-1,c[0]);
如果((检查=从)){
var filter={include:[
“品牌”,
“模型”,
“颜色”,
“carType”,
“收入”,
“收入日”,
“ins_cpy”,
“ins_cpy”,
“调查日期”,
“汽车照片”,
'所有者名称\公司名称'],
字段:[
“id”、“品牌”,
“模型”,
“颜色”,
“carType”,
“收入”,
“收入日”,
“ins_cpy”,
“ins_cpy”,
“调查日期”,
“汽车照片”,
'ownerNICId_companyName'],其中:{和:[{location:location},{carType:carType},{startDate:d1},{endDate:d2},{status:1}]};
}
Bookingcar.find(位置、开始日期、结束日期、类型、{
包括:[{
关系:'car'}
]},
功能(错误、结果){
如果(错误)
控制台日志(err);
否则{
回调(空,结果);
}
});
};
Bookingcar.remoteMethod(
“findCar”{
http:{path:'/findCar',动词'get'},
接受:[
{arg:'location',键入:'string'},
{arg:'startDate',键入:'date'},
{arg:'endDate',键入:'date'},
{arg:'carType',type:'string'}
],
返回:[
{arg:'id',type:'number',description:'id',required:true,root:true},
{arg:'brand',type:'string',required:true,root:true},
{arg:'model',type:'string',必需:false,root:true},
{arg:'color',type:'string',required:true,root:true},
{arg:'carType',type:'string',必需:false,root:true},
{arg:'regiNo',类型:'string',必需:true,root:true},
{arg:'revenue_li_cpy',类型:'string',必需:false,root:true},
{arg:'revenue\u li\u ex\u date',类型:'date',必需:true,root:true},
{arg:'ins_cpy',类型:'string',必需:false,root:true},
{arg:'ins_cpy',类型:'string',必需:true,根:true},
{arg:'ins_exp_date',类型:'date',必需:false,root:true},
{arg:'car_photo',类型:'string',必需:true,根:true},
{arg:'ownerNicd_companyName',类型:'string',必需:false,root:true},
]
}
);
};
但它的工作和我有暧昧是否
BookingCar.findCar=函数(位置、开始日期、结束日期、类型、回调)
这是对还是错。因为他所有的引用都只针对一个论点
请给出解决我的问题的想法或提示以下是一个基于代码的远程方法示例:
'use strict';
module.exports = function(Bookingcar) {
BookingCar.findCar = function(location, carType) {
return new Promise((resolve, reject) => {
const filter = {
include: ['car'], // Include the car relation
where: {
and: [
{location: location},
{carType: carType},
or: [
{startDate: {gt: Date.now()}},
{endDate: {lt: Date.now()}}
]
]
}
};
Bookincar.find(filter, (err, results) => {
if (err) return reject(err);
// you can add more logic on the results here if needed
resolve(results);
});
});
});
Bookingcar.remoteMethod(
'findCar', {
http: {path: '/findCar', verb: 'get'},
accepts: [
{arg: 'location', type: 'string', required: true},
{arg: 'carType', type: 'string', required: true}
],
returns: {arg: 'cars', type: 'array'}=
}
);
我构建的过滤器意味着:给我所有匹配的值
- 确切位置和
- 确切的carType和
- (开始日期在现在之后或结束日期在现在之前)
startDate
和endDate
参数不是必需的,因为您必须将Bookingcar
实例中包含的参数与now()进行比较
最后,结果将是一个实例数组(对象数组),您不必在远程方法定义的返回值中详细说明
但是,基本上,在这里,您不需要构建远程方法。您只需使用此筛选器查询环回,因为远程方法中没有特定的逻辑。首先,使用var BookingCar=require('./BookingCar.json')代码>,您正在重写loopback的模型BookingCar。第二,您构建了一个未使用的筛选器,第三,您是Bookingcar.find(location,startDate,endDate,carType,{object])
不正确,它应该只是Bookingcar(filter,(err,results)=>{…}
。第四:你dateCheck
从哪里来?dateCheck是当前日期,先生,我完全搞不懂。如果你有任何编写远程方法的链接教程,请告诉我先生,但问题是我必须从'car model'sir'获取可用的汽车详细信息。它在包括过滤器的一部分:你的预订汽车实例将我带来了汽车的细节。