【问题标题】:mongoose not fetching data猫鼬不获取数据
【发布时间】:2020-03-10 08:27:20
【问题描述】:

我正在尝试从 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() 没有检索到任何内容。可能是什么原因?

提前致谢。

【问题讨论】:

    标签: mongodb mongoose mean


    【解决方案1】:

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

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

    或更好

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

    并在你想使用它的地方要求它

    const Employee = require('path to the schema file')
    

    【讨论】:

    • 我已经尝试过了,但没有成功,奇怪的是它正在工作,但是当我发送文件并在另一台 PC 上构建项目时它坏了......
    • 是否可以指定可以连接到数据库的ip?
    猜你喜欢
    • 2020-08-30
    • 2022-12-19
    • 2016-07-30
    • 2016-11-12
    • 2016-01-21
    • 2016-05-05
    • 2016-12-12
    • 2016-01-15
    • 1970-01-01
    相关资源
    最近更新 更多