猫鼬未获取数据

问题描述 投票:0回答:1

我正在尝试从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:[email protected]/employee?retryWrites=true&w=majority")
  .then(() => {
    console.log("Connected")
  })

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

提前感谢。

mongodb mongoose mean
1个回答
0
投票

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

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

或更好

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

并在需要使用它的地方要求它

const Employee = require('path to the schema file')
© www.soinside.com 2019 - 2024. All rights reserved.