I don't know whats happen here, I'm just using async await:
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;
this return an error:
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) at next (D:\curso\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D:\curso\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (D:\curso\node_modules\express\lib\router\layer.js:95:5) at D:\curso\node_modules\express\lib\router\index.js:281:22 at Function.process_params (D:\curso\node_modules\express\lib\router\index.js:335:12) at next (D:\curso\node_modules\express\lib\router\index.js:275:10) at Function.handle (D:\curso\node_modules\express\lib\router\index.js:174:3) at router (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) at D:\curso\node_modules\express\lib\router\index.js:284:7 at Function.process_params (D:\curso\node_modules\express\lib\router\index.js:335:12) at next (D:\curso\node_modules\express\lib\router\index.js:275:10) at jsonParser (D:\curso\node_modules\body-parser\lib\types\json.js:110:7)
Why is find not a function?
This is the model:
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
in../models/employee
doesn't have such a method. – blockhead