Я не знаю, что здесь происходит, я просто использую 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;
это возвращает ошибку:
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);