1
Routes:
const express = require('express');
const router = express.Router();
const controller = require('../controllers/imoveis-controller');
router.get('/:id', controller.getById);
router.get('/', controller.getAll);
router.post('/', controller.post);
router.put('/:id', controller.put);
router.delete('/', controller.delete);
module.exports = router;
Controller:
const sequelize = require('sequelize');
const database = require('../config/dbconnection');
const model = require('../models/imoveis');
const repository = require('../repositories/property_repository');
exports.getAll = repository.getAll;
exports.getById = repository.getById;
Repository
const dbConnect = require('../config/dbconnection');
const model = require('../models/imoveis');
exports.getAll = (req, res, next) => model.findAll()
.then((result) => {
res.send(result);
})
.catch(error => console.log('Erro: ' + errror));
exports.getById = (req, res, next) => model.findByPk(res.params.id)
.then((result) => {
res.send(result);
})
.catch(error => console.log('getById error: ' + error));
When I call the Getall method works normally, but when I call the getById method and pass the Id by the url it gives the error: Typeerror: Cannot read Property 'id' of Undefined
and I’m using the body-parser:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
has some other way to retrieve the id and pass it as parameter to the Repository?