【问题标题】:Error while querying MongoDB database by Parameter with a Nodejs ExpressJS backend --使用 Nodejs ExpressJS 后端通过参数查询 MongoDB 数据库时出错——
【发布时间】:2019-09-24 04:53:42
【问题描述】:

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

??

我完全不知所措。到底哪里不对?

【问题讨论】:

    标签: node.js mongodb express nosql


    【解决方案1】:

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

    【讨论】:

    • 嗨@Tiago,那个 player.model.js 不是可以使用的模型吗?我必须再做一个新的分开?那应该怎么样?你能暗示一个样本吗?名人堂模型的一些代码?我很困惑
    • 你刚刚为玩家创建了一个模式,所以它只有玩家信息。您现在需要创建一个实际的播放器作为模型,我发送的链接应该非常简单。
    • 嗨,是的,许多玩家记录已经添加/创建到 MongoDB 数据库中,我已经完成了那部分。现在我正在尝试查询它们,这就是它失败的地方...... @Tiago
    • 'Hall of Fame' 或 ' ' 是我在 player_ 下保存的值,如果你观察那个玩家模式的话。希望您能理解我要解释的内容,
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2020-12-31
    相关资源
    最近更新 更多