I used the filter function to POST and search the result using hardcoded data. Then inserted the data in MongoDB and using GET got the entire data. Then I used POST on MongoDB to search results but I get an error saying filter is undefined.
Controllers file data
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
});
};
Schema Used :
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');
Error received in the console log is below.
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