【问题标题】:Show one element of a mongoDB collection显示 mongoDB 集合的一个元素
【发布时间】:2016-10-09 21:37:53
【问题描述】:

我有 mongodb 集合,我需要从中提取高中和大学到各自的路线。 我的收藏是这样的

{ “_id”:“1f24ef0c-6f9c-478a-adc9-7c55d7708930”, “高中”:“现代修道院”, “学院”:“史蒂文斯理工学院” }

我的路线如下所示:-

const data = require("../data");
const educationData = data.education;

    router.get("/highschool", (req, res) => {
        educationData.getHighSchool().then((education) => {
            res.json(education);
        }, () => {
            // Not found!
            res.sendStatus(404);
        });
    });

    router.get("/", (req, res) => {
        educationData.getAllEducation().then((EducationList) => {
            res.json(EducationList);
        }, () => {
            // Something went wrong with the server!
            res.sendStatus(500);
        });
    });

我的“/”路线返回 id、大学和高中。但是在“/highschool”中,我只想提取集合中存在的高中的名称。我怎样才能做到这一点。目前我正在使用以下函数来返回数据。

    const education = mongoCollections.education;// added for reference

    return education().then((educationCollection) => {
            return educationCollection.find({}).toArray();
        });
    },

    getHighSchool(){
        return education().then((educationCollection)=> {
            return educationCollection.highSchool;
        });
    }

“getAllEducation”函数返回所有内容,但“getHighSchool”没有返回任何内容。

【问题讨论】:

  • 你的观点是谁?您的代码中缺少getAllEducation,对吗?

标签: node.js mongodb express es6-promise


【解决方案1】:

getHighScholl 方法中缺少查询

getHighSchool(){
    return education().then((educationCollection)=> {
        return educationCollection.findOne({}).highSchool;
    });
}

【讨论】:

  • 我尝试使用它,但我的网页没有显示任何内容。既不是输出也不是“内部服务器”或“未找到”错误。 @MarceloRisse
  • @ArjunDass 如果您将服务器的响应打印到浏览器的控制台,您能否发布显示的内容?
猜你喜欢
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
相关资源
最近更新 更多