Skip to content
Advertisement

Error: Route.get() requires a callback function but got a [object Undefined] at app.js

I dont know why off this error I have another model and almost the same route and the controllers but is not working this is the message: Error: Route.get() requires a callback function but got a [object Undefined] at Route. [as get] (/Users/ediechavero/Documents/Recipe-API/node_modules/express/lib/router/route.js:202:15) at Function.proto. [as get] (/Users/ediechavero/Documents/Recipe-API/node_modules/express/lib/router/index.js:510:19) at Object. (/Users/ediechavero/Documents/Recipe-API/routes/places-routes.js:22:8) at Module._compile (internal/modules/cjs/loader.js:1156:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10) at Module.load (internal/modules/cjs/loader.js:1000:32) at Function.Module._load (internal/modules/cjs/loader.js:899:14) at Module.require (internal/modules/cjs/loader.js:1042:19) at require (internal/modules/cjs/helpers.js:77:18) at Object. (/Users/ediechavero/Documents/Recipe-API/app.js:62:15) at Module._compile (internal/modules/cjs/loader.js:1156:30) at Object.Module._extensions..js (in

//Controllers places this is the controllers file for the crud places
const Place = require('../models/Place');

exports.getAllPlacess = async (req, res) => {
  try {
    const places = await Place.find().populate('owner');
    res.status(200).json({ places });
  } catch (error) {
    res.status(400).json({ message: `${error}` });
  }
};

exports.getPlace = async (req, res) => {
  try {
    const { id } = req.params;
    const place = await (await Place.findById(id)).populate('owner');
    res.status(200).json({ place });
  } catch (error) {
    res.status(400).json({ message: `${error}` });
  }
};

exports.createPlace = async (req, res) => {
  try {
    const { name, typePlace, address } = req.body;
    const owner = req.user.id;

    const place = await Place.create({
      name,
      typePlace,
      address,
      owner,
    });
    res.status(201).json({ place });
  } catch (error) {
    res.status(400).json({ message: `${error}` });
  }
};

exports.updatePlace = async (req, res) => {
  try {
    const { id } = req.params;
    const { name, typePlace, address } = req.body;
    const place = await Place.findByIdAndUpdate(id, {
      name,
      typePlace,
      address,
      owner,
    });
    res.status(200).json({ place });
  } catch (error) {
    res.status(400).json({ message: `${error}` });
  }
};

exports.deletePlace = async (req, res) => {
  try {
    const { id } = req.params;
    await Place.findByIdAndDelete(id);
    res.status(200).json({ message: 'Deleted Recipe' });
  } catch (error) {
    res.status(400).json({ message: `${error}` });
  }
};



//Route this the file for the routes 
const express = require('express');
const router = express.Router();
const uploadConfig = require('../config/cloudinary');

const {
  getAllPlaces,
  getPlace,
  createPlace,
  updatePlace,
  deletePlace,
} = require('../controllers/places');

router.post('/upload', uploadConfig.single('photo'), (req, res, next) => {
  if (!req.file) {
    next(new Error('No file uploades'));
  }
  res.status(201).json({ secure_url: req.file.secure_url });
});

//CRUD PLACES

router.get('/places', getAllPlaces);
router.get('/places/:id', getPlace);
router.post('/places/create', createPlace);
router.put('/places/edit/:id', updatePlace);
router.delete('/places/delete/:id', deletePlace);

module.exports = router;

//app.js

require('dotenv').config();

const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const express = require('express');
const mongoose = require('mongoose');
const logger = require('morgan');
const path = require('path');
const colors = require('colors');
const cors = require('cors');
const session = require('express-session');

mongoose
  .connect(process.env.DB, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
  })
  .then((x) =>
    console.log(
      `Connected to Mongo! Database name: "${x.connections[0].name}"`.cyan
        .underline.bold
    )
  )
  .catch((err) => console.error('Error connecting to mongo', err.red));

const app_name = require('./package.json').name;
const debug = require('debug')(
  `${app_name}:${path.basename(__filename).split('.')[0]}`
);

const app = express();

app.use(
  cors({
    credentials: true,
    origin: [process.env.FRONTENDPOINT],
  })
);

app.use(
  session({
    resave: false,
    saveUninitialized: true,
    secret: process.env.SECRET,
    cookie: { maxAge: 1000 * 60 * 60 },
  })
);

app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(logger('dev'));

const index = require('./routes/index');
app.use('/', index);

const recipe = require('./routes/recipe-routes');
app.use('/api', recipe);

const place = require('./routes/places-routes');
app.use('/api', place);

// Uncomment this line for production
// app.get('/*', (req, res) => res.sendFile(__dirname + '/public/index.html'));

module.exports = app;

Advertisement

Answer

It looks like there’s a difference in spelling where you export and where you import.

You are defining one export as this:

exports.getAllPlacess = function() { ... }

But, then importing it as this:

const {
  getAllPlaces,
  getPlace,
  createPlace,
  updatePlace,
  deletePlace,
} = require('../controllers/places');

Change this:

exports.getAllPlacess = function() { ... }

to this:

exports.getAllPlaces = function() { ... }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement