我试图查询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”,这就是我要查询的内容。那么这是什么问题呢?哪里出错了?我在名人堂周围用单引号和双引号进行了试验,以确保问题不存在。当我用Postman测试后端并向 localhost:4000 / playerDB / hallOfFame 发出GET请求时,我简直是一片空白。甚至都不为空。 但值得注意的是状态显示为:200 OK
??
我完全茫然。到底有什么不对?