在带有Nodejs ExpressJS后端的参数查询MongoDB数据库时出错-

时间:2019-09-23 15:44:30

标签: node.js mongodb express nosql

我试图查询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

??

我完全茫然。到底有什么不对?

1 个答案:

答案 0 :(得分:0)

您在那里有架构,但是没有该架构的模型。尝试为名人堂球员建模。 https://mongoosejs.com/docs/models.html