got this output on cmd and localhost "createError is not defined"

Viewed 3626

hello all i am new to node js, mongo db. I create a crud app using this link :https://www.positronx.io/mean-stack-tutorial-angular-7-crud-bootstrap/. I dont know but why i got this output on cmd and localhost "createError is not defined". Please help me as i am a beginner in node.

server.js

let express = require('express'),
   path = require('path'),
   mongoose = require('mongoose'),
   cors = require('cors'),
   bodyParser = require('body-parser'),
   dbConfig = require('./database/db');

// Connecting with mongo db
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/meandb", {
   useNewUrlParser: true
}).then(() => {
      console.log('Database sucessfully connected')
   },
   error => {
      console.log('Database could not connected: ' + error)
   }
)

// Setting up port with express js
const employeeRoute = require('../backend/routes/employee.route')
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
   extended: false
}));
app.use(cors()); 
app.use(express.static(path.join(__dirname, 'dist/mean-stack-crud-app')));
app.use('/', express.static(path.join(__dirname, 'dist/mean-stack-crud-app')));
app.use('/api', employeeRoute)

// Create port
const port = process.env.PORT || 4000;
const server = app.listen(port, () => {
  console.log('Connected to port ' + port)
})

// Find 404 and hand over to error handler
app.use((req, res, next) => {
   next(createError(404));
   
});

// error handler
app.use(function (err, req, res, next) {
   
  console.error(err.message); // Log error message in our server's console
  if (!err.statusCode) err.statusCode = 500; // If err has no specified error code, set error code to 'Internal Server Error (500)'
  res.status(err.statusCode).send(err.message); // All HTTP requests must have a response, so let's send back an error with its status code and message
});

Employee.route.js

const express=require('express');
const app=express();
const employeeRoute=express.Router();

//Employee model
let Employee=require('../models/Employee');

//Add Employee
employeeRoute.route('/create').post((req,res,next)=>{
    Employee.create(req.body,(error,data)=>{
        if(error){
            return next(error)
        }
        else{
            res.json(data)
        }
    })
});

//Get ALL Employee
employeeRoute.route('/').get((req,res)=>{
    Employee.find((error,data)=>{
        if(error){
            return next(error)
        }
        else{
            res.json(data)
        }
    })
})
// Get single employee
employeeRoute.route('/read/:id').get((req, res) => {
    Employee.findById(req.params.id, (error, data) => {
      if (error) {
        return next(error)
      } else {
        res.json(data)
      }
    })
  })
  
  
  // Update employee
  employeeRoute.route('/update/:id').put((req, res, next) => {
    Employee.findByIdAndUpdate(req.params.id, {
      $set: req.body
    }, (error, data) => {
      if (error) {
        return next(error);
        console.log(error)
      } else {
        res.json(data)
        console.log('Data updated successfully')
      }
    })
  })
  
  // Delete employee
  employeeRoute.route('/delete/:id').delete((req, res, next) => {
    Employee.findOneAndRemove(req.params.id, (error, data) => {
      if (error) {
        return next(error);
      } else {
        res.status(200).json({
          msg: data
        })
      }
    })
  })
module.exports=employeeRoute;

And in angular section, when i tried to submit data, submit button not do anything.

1 Answers

Will take some getting used to but this error usually means you do not have a variable named 'createError' created elsewhere. That variable is commonly used the way you have it for the 'http-errors' npm package. You can learn more about that here: https://www.npmjs.com/package/http-errors

Add this to the top of your app.js file.

App.js

const createError = require('http-errors');

Also make sure you have the package installed and saved with this command. Run this in the same directory as your package.json file.

npm install http-errors --save
Related