javascript - 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);
解决方案
您没有从模型中导出任何内容。您需要像这样导出它:
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()
返回一个Promise
:https ://mongoosejs.com/docs/api.html#query_Query-exec
employeeCtrl.getEmployees = async (req, res) => {
const employees = await Employee.find().exec();
res.json(employees);
}
推荐阅读
- python - scipy.integrate solve_ivp 理解例子
- python - Python:如何将 android.show_keyboard 用于我的 android 应用程序?
- c# - 如何更改 goto 语句以改进代码?
- reactjs - 使用 console.log 时 React Hook 不返回任何内容
- swift - 如何正确使用 .dropLast()
- android - 如何防止 SharedPreferences 加载损坏的数据
- javascript - Js 按钮单击替代
- ios - 联系
: 无法在没有连接的情况下确定回退状态 - vue.js - 如何在设置中调用 recaptcha 实例?
- python - Cisco Packet Tracer 中的 Python 套接字编程