带有Nodejs ExpressJS后端的参数查询MongoDB数据库时出错
我正在尝试查询我的mongoDB数据库playerDB,并编写了一个nodeJS和基于expressJS的后端来查询数据库。让我发布相关代码。
这是我试图在server.js中形成查询的部分->
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const PORT = 4000;
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const playerRoutes = express.Router();
app.use(cors());
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false
})
);
let Player = require('./player.model');
var query = Player.find({ player_: "Hall of Fame" });
playerRoutes.route('/hallOfFame').get(function (req, res) {
query.exec(function (err, players) {
if (err) {
return handleError(err);
}
else {
res.json(players);
}
});
});
这是我的player.model.js-
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let Player = new Schema({
player_name: {
type: String
},
player_description: {
type: String
},
player_position: {
type: String
},
player_age: {
type: String
},
player_club: {
type: String
},
player_: {
type: String
},
player_isactive: {
type: Boolean
},
player_completed: {
type: Boolean
}
});
module.exports = mongoose.model('player', Player);
基本上我的player_属性应该具有值'Hall of Fame',这就是我要查询的内容。那么这是什么问题呢?哪里出错了?我在Hall of Fame周围用单引号和双引号进行了试验,以确保问题不存在。当我用Postman测试后端并向localhost:4000 / playerDB / hallOfFame发出GET请求时,我只是变得一片空白。甚至都不为空。 但是值得注意的是状态显示:200 OK
??
我完全茫然。到底什么是不正确的?
回答如下:您在那里有架构,但没有该架构的模型。尝试为名人堂球员建模。 https://mongoosejs/docs/models.html