首页 > 解决方案 > 尝试 findAll() 时集合返回空。RESTful 和 Mongodb

问题描述

我正在尝试使用 REST、NodeJS、ExpressJS 和 MongoDB 地图集实现 RESTful API。我已成功连接我的地图集数据库,我正在尝试从“个人信息”集合中检索所有内容。

我的图集云集合如下所示:

_id:ObjectId("60884283fd31d723047c6f7c")
customerID:"6543524356"
Title:"Ms"
Fname:"Anna"
Lname:"Dunne"
mobile:"0764323456"
email:"anna@here.ie" 

我的 server.js 成功连接到我的云数据库

我的 model.js 看起来像这样:

 const mongoose = require('mongoose');
// create a mongoose schema for a quotation
const PersonalInfoSchema = mongoose.Schema({
   customerID: String,
   Title: String,
   Fname: String,
   Lname: String,
   mobile: String,
   email: String
  },  {
    timestamps: true
 });
 // export the model to our app
  module.exports = mongoose.model('Personal_information', PersonalInfoSchema);

这是我的 controller.js 的一部分,假设可以在我的个人信息中找到所有文档

  exports.findAll = (req, res) => {
     Personal_information.find()
       .then(personal_information => {
           res.send(personal_information);
        }).catch(err => {
           res.status(500).send({
               message: err.message || "An error occurred while retrieving all personal information."
           });
       });
};

我已将路线定义为

    module.exports = (app) => {
       app.get('/personal_information', personal_information.findAll);
     }

但是当我在邮递员中运行 url 时,它返回空,即使我的个人信息集合中有 3 个文档。任何想法为什么以及如何解决它?

标签: node.jsmongodbexpress

解决方案


推荐阅读