我想在 Model.find() 方法中的 where() 中添加一个函数,但它在正文中包含错字错误
I want to add a function in where() in Model.find() method but its giving typo error included in Body
提供传递路径的代码会给我一个类型错误,该错误包含在下面
function maxDis(lat1, lon1, location) {
var fields = location.split(':');
var lat2 = parseFloat(fields[0]);
var lon2 = parseFloat(fields[1]);
var maxDis = parseInt(fields[2]);
var theta = lon1 - lon2;
var dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60; // 60 nautical miles per degree of seperation
dist = dist * 1852; // 1852 meters per nautical mile
if (Math.abs(dist) < maxDis) {
return true;
} else {
return false;
}
}
这里也出现错误:
resRouter.route('/locationfilters/veg/:location')
.options(cors.corsWithOptions, (req, res) => { res.sendStatus(200) })
.get(cors.cors, (req, res, next) => {
Res.find({})
.where(maxDis(parseFloat(this.latitude), parseFloat(this.longitude), req.params.location) == true)
.then((rest) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(rest);
}, (err) => next(err))
.catch((err) => next(err));
});
我无法找到不是字符串类型的路径
执行请求时的错误日志 ->
TypeError: path must be a string or object
at model.Query.Query.where (/home/yash/Documents/FoodApp/conFusionServer/node_modules/mquery/lib/mquery.js:299:9)
at resRouter.route.options.get (/home/yash/Documents/FoodApp/conFusionServer/routes/resRouter.js:351:14)
at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:137:13)
at cors (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:188:7)
at /home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:224:17
at originCallback (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:214:15)
at /home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:219:13
at optionsCallback (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:204:7)
at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:137:13)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:131:14)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:131:14)
at Route.dispatch (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
您需要 Model.$where()
method instead of Query.where()
. Since your query is using a JavaScript expression, you can either do so via Model.find({ $where: <javascript_function> })
, or use the mongoose shortcut method $where
通过查询链或来自您的猫鼬模型,其中参数是 javascript 字符串或匿名函数,即
Res.$where(() => maxDis(parseFloat(this.latitude), parseFloat(this.longitude), req.params.location))
.then((rest) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(rest);
}, (err) => next(err))
.catch((err) => next(err));
提供传递路径的代码会给我一个类型错误,该错误包含在下面
function maxDis(lat1, lon1, location) {
var fields = location.split(':');
var lat2 = parseFloat(fields[0]);
var lon2 = parseFloat(fields[1]);
var maxDis = parseInt(fields[2]);
var theta = lon1 - lon2;
var dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60; // 60 nautical miles per degree of seperation
dist = dist * 1852; // 1852 meters per nautical mile
if (Math.abs(dist) < maxDis) {
return true;
} else {
return false;
}
}
这里也出现错误:
resRouter.route('/locationfilters/veg/:location')
.options(cors.corsWithOptions, (req, res) => { res.sendStatus(200) })
.get(cors.cors, (req, res, next) => {
Res.find({})
.where(maxDis(parseFloat(this.latitude), parseFloat(this.longitude), req.params.location) == true)
.then((rest) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(rest);
}, (err) => next(err))
.catch((err) => next(err));
});
我无法找到不是字符串类型的路径
执行请求时的错误日志 ->
TypeError: path must be a string or object
at model.Query.Query.where (/home/yash/Documents/FoodApp/conFusionServer/node_modules/mquery/lib/mquery.js:299:9)
at resRouter.route.options.get (/home/yash/Documents/FoodApp/conFusionServer/routes/resRouter.js:351:14)
at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:137:13)
at cors (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:188:7)
at /home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:224:17
at originCallback (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:214:15)
at /home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:219:13
at optionsCallback (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:199:9)
at corsMiddleware (/home/yash/Documents/FoodApp/conFusionServer/node_modules/cors/lib/index.js:204:7)
at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:137:13)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:131:14)
at next (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:131:14)
at Route.dispatch (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/yash/Documents/FoodApp/conFusionServer/node_modules/express/lib/router/layer.js:95:5)
您需要 Model.$where()
method instead of Query.where()
. Since your query is using a JavaScript expression, you can either do so via Model.find({ $where: <javascript_function> })
, or use the mongoose shortcut method $where
通过查询链或来自您的猫鼬模型,其中参数是 javascript 字符串或匿名函数,即
Res.$where(() => maxDis(parseFloat(this.latitude), parseFloat(this.longitude), req.params.location))
.then((rest) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(rest);
}, (err) => next(err))
.catch((err) => next(err));