猫鼬不获取数据

时间:2019-11-14 11:29:39

标签: mongodb mongoose mean

我正在尝试从MongoDB数据库中获取所有用户。但是,由于某种原因,该请求最近没有获取任何内容。

这是我尝试获取数据的代码:

app.get('/api/allusers', (req, res) => {
  Employee.find()
    .then(rettrievedData => {
      res.json(rettrievedData)
  });
});

这是猫鼬模型:

const mongoose = require('mongoose');

const employeeSchema = mongoose.Schema({

    name: { type: String },
    surName: { type: String },
    mail: { type: String },
    phone: { type: String },
});

module.exports = mongoose.model('Employee', employeeSchema, 'employee.employees');

这是用于连接Mongo的代码

mongoose.connect("mongodb+srv://Kiril:xxxxxxxxxxxxx@cluster0-owdfy.mongodb.net/employee?retryWrites=true&w=majority")
  .then(() => {
    console.log("Connected")
  })

我还检查了数据库中是否有数据,但是由于某种原因,Employee.find()没有检索到任何数据。可能是什么原因?

谢谢。

1 个答案:

答案 0 :(得分:0)

为什么在创建模型时添加“ employee.employyes” 尝试导出没有模型的模型

module.exports = mongoose.model('Employee', employeeSchema)

或更佳

exports.Employee = mongoose.model('Employee', employeeSchema)

,并在您要使用它的地方要求它

const Employee = require('path to the schema file')
相关问题