【问题标题】:Express: problem using async await in a controllerExpress:在控制器中使用异步等待的问题
【发布时间】:2019-05-22 21:14:46
【问题描述】:

我不知道这里发生了什么,我只是在使用异步等待:

const Employee = require('../models/employee');

const employeeCtrl = {};

employeeCtrl.getEmployees = async (req, res) => {
  const employees = await Employee.find();
  res.json(employees);
}

employeeCtrl.createEmployee = async (req,res) => {
  const employee = new Employee(req.body)
  console.log(employee);
  await employee.save();
  res.json('recivied');
}

employeeCtrl.getEmployee = function() {

}

employeeCtrl.editEmployee = function() {

}

employeeCtrl.deleteEmployee = function() {

}

module.exports = employeeCtrl;

这会返回一个错误:

TypeError: Employee.find 不是函数 在employeeCtrl.getEmployees (D:\curso\server\controllers\employee.controller.js:6:31) 在 Layer.handle [as handle_request] (D:\curso\node_modules\express\lib\router\layer.js:95:5) 在下一个 (D:\curso\node_modules\express\lib\router\route.js:137:13) 在 Route.dispatch (D:\curso\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (D:\curso\node_modules\express\lib\router\layer.js:95:5) 在 D:\curso\node_modules\express\lib\router\index.js:281:22 在 Function.process_params (D:\curso\node_modules\express\lib\router\index.js:335:12) 在下一个 (D:\curso\node_modules\express\lib\router\index.js:275:10) 在 Function.handle (D:\curso\node_modules\express\lib\router\index.js:174:3) 在路由器 (D:\curso\node_modules\express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request] (D:\curso\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (D:\curso\node_modules\express\lib\router\index.js:317:13) 在 D:\curso\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (D:\curso\node_modules\express\lib\router\index.js:335:12) 在下一个 (D:\curso\node_modules\express\lib\router\index.js:275:10) 在 jsonParser (D:\curso\node_modules\body-parser\lib\types\json.js:110:7)

为什么 find 不是函数?

这是模型:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const EmployeeSchema = new Schema({
  name: {type: String, required: true},
  position: {type: String, required: true},
  office: {type: String, required: true},
  salary: {type: Number, required: true}
})

mongoose.model('Employee', EmployeeSchema);

【问题讨论】:

  • 可能是因为Employee中的../models/employee没有这样的方法。
  • @blockhead 我添加了文件 ../models/employee 你可以检查一下吗?
  • 你没有导出你的模型

标签: javascript node.js express mongoose async-await


【解决方案1】:

从您的代码示例中,您似乎没有导出模型。也许在models/Employee 试试这个:

module.exports = mongoose.model('Employee', EmployeeSchema);

【讨论】:

    【解决方案2】:

    我认为问题在于您没有导出刚刚创建的架构。

    试试这个

    module.exports = mongoose.model('Employee', EmployeeSchema);
    

    不仅仅是这个

    mongoose.model('Employee', EmployeeSchema);
    

    【讨论】:

      【解决方案3】:

      您没有从模型中导出任何内容。你需要像这样导出它:

      const mongoose = require('mongoose');
      const { Schema } = mongoose;
      
      const EmployeeSchema = new Schema({
        name: {type: String, required: true},
        position: {type: String, required: true},
        office: {type: String, required: true},
        salary: {type: Number, required: true}
      })
      
      module.exports = mongoose.model('Employee', EmployeeSchema);
      

      此外,.find() 不会返回 Promise。它返回一个 Query 对象,如文档中所述:https://mongoosejs.com/docs/api.html#model_Model.find

      你需要用.exec()链接它,它返回一个Promisehttps://mongoosejs.com/docs/api.html#query_Query-exec

      employeeCtrl.getEmployees = async (req, res) => {
        const employees = await Employee.find().exec();
        res.json(employees);
      }
      

      【讨论】:

        猜你喜欢
        • 2012-10-14
        • 1970-01-01
        • 2018-03-12
        • 2021-10-11
        • 1970-01-01
        • 2018-12-09
        • 2019-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多