首页 > 解决方案 > Express:在控制器中使用异步等待的问题

问题描述

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

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 is not a function at employeeCtrl.getEmployees (D:\curso\server\controllers\employee.controller.js:6:31) at 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) at Layer.handle [as handle_request] (D:\curso\node_modules\express\lib\router\layer.js:95:5) at 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:\光标\node_modules\body-parser\lib\types\json.js:110:7)\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)\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);

标签: javascriptnode.jsexpressmongooseasync-await

解决方案


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

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);
}

推荐阅读