我不知道这里发生了什么,我只是在使用异步等待:
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上[作为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上[作为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上[作为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)
为什么找不到功能?
这是模型:
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);
答案 0 :(得分:2)
您没有从模型中导出任何内容。您需要像这样导出它:
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);
}
答案 1 :(得分:1)
我认为问题在于您没有导出刚刚创建的架构。
尝试
module.exports = mongoose.model('Employee', EmployeeSchema);
不仅仅是这个
mongoose.model('Employee', EmployeeSchema);
答案 2 :(得分:0)
从您的代码示例中,您似乎没有导出模型。也许在models/Employee
中尝试一下:
module.exports = mongoose.model('Employee', EmployeeSchema);