Reputation: 1658
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);
Upvotes: 1
Views: 2904
Reputation: 670
From your code sample, it doesn't appear you're exporting the model. Perhaps try this in models/Employee
:
module.exports = mongoose.model('Employee', EmployeeSchema);
Upvotes: 1
Reputation: 11860
I think the problem is that your are not exporting the schema you just made.
Try this
module.exports = mongoose.model('Employee', EmployeeSchema);
Instead of just this
mongoose.model('Employee', EmployeeSchema);
Upvotes: 1
Reputation: 22972
You are not exporting anything from your model. You need to export it like so:
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);
Additionally, .find()
does not return a Promise
. It returns a Query
object as stated in the docs: https://mongoosejs.com/docs/api.html#model_Model.find
You need to chain it with .exec()
which returns a Promise
: https://mongoosejs.com/docs/api.html#query_Query-exec
employeeCtrl.getEmployees = async (req, res) => {
const employees = await Employee.find().exec();
res.json(employees);
}
Upvotes: 3