I'm new to NodeJS and I'm trying to make a backend API for a car rental agency. I have defined the type of 'name' as string and also I'm not passing an empty string in the request then too why is it showing this error? I'm searching for the solution since days but cannot find the answer anywhere on the internet. I read somewhere the older version of mongoose had a bug so I tried after updating it then too the error is not going and also tried updating to the latest NodeJS version.
Here is my code,
index.js
let express = require('express');
let app = express();
let mongoose = require('mongoose');
var port = process.env.PORT || 8081
let bodyparser = require('body-parser');
const { urlencoded } = require('body-parser');
app.use(bodyparser.urlencoded(
{
extended: true
}))
app.use(bodyparser.json());
var apiRoutes = require('./routes');
app.use('/api', apiRoutes);
const dbPath = "mongodb://localhost/carrentalagencypractice"
const options = {useNewUrlParser:true, useUnifiedTopology:true}
const mongo = mongoose.connect(dbPath,options)
mongo.then(()=>{
console.log('Connected to mongodb')
} ,error=> {console.log(error,'error')})
app.use('/', function(req, res){
res.send('Welcome to Car Rental Agency')
});
app.listen(port, ()=>{
console.log('Running on port '+port)
});
routes.js
let router = require('express').Router();
router.get('/', function(req, res){
res.json({
status: 'API works',
message: 'Welcome to Car Rental Agency services'
})
})
var carController = require('./carController')
var userController = require('./userController')
router.route('/car').get(carController.index).post(carController.add)
router.route('/user').get(userController.index).post(userController.add)
router.route('/car/car_id').get(carController.view).patch(carController.update).put(carController.update).delete(carController.delete)
router.route('/user/user_id').get(userController.index).patch(userController.update).put(userController.update).delete(userController.delete)
module.exports = router
carModel.js
var mongoose = require('mongoose')
var carSchema = new mongoose.Schema({
name:{
type: String,
required: true
},
brand:{
type: String,
required: true
},
price:{
type: String,
required: true
},
capacity:{
type: String,
required:true
}
});
var Car = module.exports = mongoose.model('car',carSchema);
module.exports.get = function(callback, limit){
Car.find(callback).limit(limit);
}
userModel.js
var mongoose = require('mongoose')
var userSchema = new mongoose.Schema({
name:{
type: String,
required: true
},
email:{
type: String,
required: true
},
phone:{
type: String,
required: true
},
address:{
type: String,
required: true
}
})
var User = module.exports = mongoose.model('user', userSchema)
module.exports.get = function(callback, limit){
User.find(callback).limit(limit);
}
userController.js
User = require("./userModel");
exports.index = function(req, res){
User.get(function(err, user){
if(err)
res.send(err)
res.json({
status: "Got all user details successfully",
message: user
})
})
}
exports.add = function(req, res){
User.add(function(err, user){
var user = new User()
if(err)
res.send(err)
user.name = req.body.name ? req.body.name : user.name;
user.email = req.body.email;
user.address = req.body.address;
user.phone = req.body.phone;
res.json({
status: "User details are successfully added",
message: user
})
})
}
exports.view = function(req, res){
User.findByID(req.params.user_id, function(err, user){
if(err)
res.send(err)
res.json({
status: "Successfully retreived user details",
message: user
})
})
}
exports.update = function(req, res){
User.findById(req.params.user_id, function(err, user){
if(err)
res.send(err)
user.name = req.body.name? req.body.name: user.name;
user.email = req.body.email;
user.address = req.body.address;
user.phone = req.body.phone;
user.save(function(req, res){
if(err)
res.send(err)
res.json({
status: "User details updated successfully",
message: user
})
})
}
)
}
exports.delete = function(req, res){
User.deleteOne(req.params.user_id, function(err, user){
if(err)
res.send(err)
res.json({
status: "success",
message: "The user account has been deleted"
})
})
}
carController.js
Car = require('./carModel');
exports.index = function(req, res){
Car.get(function(err, car){
if(err)
res.json({
status: "error",
message: err
})
res.json({
status: "successfull",
message: "Car details succesfully retreived",
data: car
})
});
}
exports.add = function(req, res){
var car = new Car();
car.name = res.body.name ? req.body.name : car.name;
car.brand = res.body.brand;
car.price = res.body.price;
car.capacity = res.body.capacity;
car.save(function(err){
if(err)
res.json(err);
console.log
res.json({
status: "successfull",
message: "Successfully add new car details"
})
})
}
exports.view = function(req, res){
Car.findById(req.params.car_id, function(err, car){
if(err)
res.send(err)
res.json({
status: "Car details",
message: car
})
})
}
exports.update = function(req, res){
Car.findById(req.params.car_id, function(err, car){
if(err)
res.send(err)
car.name = req.body.name? req.body.name: car.name;
car.price = req.body.price;
car.brand = req.body.brand;
car.capacity = req.body.capacity;
car.save(function(err){
if(err)
res.send(err)
res.json({
status:"Car details updated successfully",
message: car
})
})
})
}
exports.delete = function(req, res){
Car.deleteOne({
_id: car_id
}, function(err, contact){
if(err)
res.send(err)
res.json({
status: "success",
message:"Car ad deleted"
})
}
)
}
It throws the following error:
TypeError: Cannot read property 'name' of undefined
at exports.add (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\carController.js:26:25)
at Layer.handle [as handle_request] (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\route.js:137:13)
at next (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\route.js:131:14)
at Route.dispatch (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\layer.js:95:5)
at C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:335:12)
at next (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:174:3)
at router (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:317:13)
at C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:335:12)
at next (C:\Work\NodeJS Practice\Car Rental Agency (Practice)\node_modules\express\lib\router\index.js:275:10)