Javascript 连接到猫鼬时,Post不工作
我使用filter函数使用硬编码数据发布和搜索结果。 然后将数据插入MongoDB,并使用GET获取整个数据。 然后我使用MongoDB上的POST搜索结果,但我得到一个错误,说过滤器未定义 控制器文件数据Javascript 连接到猫鼬时,Post不工作,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我使用filter函数使用硬编码数据发布和搜索结果。 然后将数据插入MongoDB,并使用GET获取整个数据。 然后我使用MongoDB上的POST搜索结果,但我得到一个错误,说过滤器未定义 控制器文件数据 const restaurantModel = require('../Models/restaurantData'); exports.getRestaurantData = (req,res) =>{ var cityname = req.body.city_nam
const restaurantModel = require('../Models/restaurantData');
exports.getRestaurantData = (req,res) =>{
var cityname = req.body.city_name;
var cost = req.body.cost;
var result = restaurantModel.restaurantData.filter(content => content.city_name === cityname && content.cost >= cost); //ES6 syntax used
console.log(result);
res.json({ respose : result,
CityName : cityname,
cost : cost
});
};
使用的架构:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const restaurantData = new Schema({
name : {
type : String,
required : true
},
city_name : {
type : String,
required : true
},
city : {
type : String,
required : true
},
area : {
type : String,
required : true
},
locality : {
type : String,
required : true
},
thumb : {
type : String,
required : true
},
cost : {
type : Number,
required : true
},
address : {
type : String,
required : true
},
type : {
type : Array,
required : true
},
Cuisine : {
type : Array,
required : true
}
});
module.exports = mongoose.model('restaurantdata', restaurantData , 'restaurantdata');
控制台日志中收到的错误如下
Connected to MongoDB
Server running at port : 5000
TypeError: Cannot read property 'filter' of undefined
at exports.getRestaurantData (/home/nigel/Documents/Education/API/12.11.2020/Controllers/restaurantData.js:11:49)
at Layer.handle [as handle_request] (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/layer.js:95:5)
at next (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/layer.js:95:5)
at /home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:335:12)
at next (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:174:3)
at router (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:317:13)
at /home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:335:12)
at next (/home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:275:10)
at /home/nigel/Documents/Education/API/12.11.2020/node_modules/express/lib/router/index.js:635:15
将mongoose的查询与比较运算符一起使用,如下所示:
要了解更多信息:我能够使用提供的解决方案,并将其与我的几个命令集成以获得结果。非常感谢。
const restaurantModel = require('../Models/restaurantData');
exports.getRestaurantData = async (req,res) => {
var cityname = req.body.city_name;
var cost = req.body.cost;
var result = await restaurantModel.find({
city_name: cityname,
const: { $gt: cost }
}).exec();
console.log(result);
res.json({
respose : result,
CityName : cityname,
cost : cost
});
};
exports.getRestaurantData = (req,res,next) =>{
restaurantdata.find({
"city_name": req.body.city_name,
"cost": req.body.cost
}, (err, content) => {
if (err) return next(err);
else {
res.json(content);
// or use "content" however you want.
}
});
});