gpt4 book ai didi

javascript - 连接到 Mongoose 时发布不起作用

转载 作者:行者123 更新时间:2023-12-04 08:35:22 25 4
gpt4 key购买 nike

我使用过滤器功能来发布并使用硬编码数据搜索结果。
然后将数据插入 MongoDB 并使用 GET 获取整个数据。
然后我在 MongoDB 上使用 POST 来搜索结果,但我收到一条错误消息,说过滤器未定义。
Controller 文件数据

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 的 find() 使用比较运算符查询 $gt 作为:

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
});
};

关于javascript - 连接到 Mongoose 时发布不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64823829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com