I have the following in plot.controller.js
const create = async(req, res, next) => {
try {
console.log(req, "request")
//console.log(req.body, "body")
const result = await plotService.create(req.body);
console.log(result, "result")
return res.json(result);
} catch (err) {
return next(err);
}
};
const update = async(req, res, next) => {
try {
const id = req.params.id;
const result = await plotService.update(req.body, id);
return res.json(result);
} catch (err) {
return next(err);
}
}
I have the following in plot.route.js
:
const express = require('express');
const controller = require('../controllers/plot.controller');
// Access parent params from endpoints
const router = express.Router({
mergeParams: true
}); // eslint-disable-line new-cap
router.post('/', controller.create); //working fine
router.put('/:id', controller.update); //causing issue
module.exports = router;
And the following in plot.service.js
:
const dbService = require('../common/db.service');
/**
* @param {String} id
* @returns {String}
*/
/**
* @param {object} object of fields to search by
* @return {promise} user model || error
*/
const create = (params) => {
return dbService.create(dbService.Plot, params);
};
const update = (params, id) => {
return dbService.update(dbService.Plot, id, params);
};
module.exports = {
create,
update
};
And the following in db.service.js
/**
* Create a new row in the database
* @param {object} Model type
* @param {object} data to save
* @return {object} DB Model
*/
const create = (Model, data) => {
return Model.forge(data).save(null).catch(SQLError);
};
/**
* Update a row in the database
* @param {object} Model type
* @param {int} id of row
* @param {object} data to save
* @return {object} DB Model
*/
const update = (Model, id, data) => {
return new Model({ id }).save(data).catch(SQLError);
};
module.exports = {
create,
update,
...Models
};
This is how I make the post request which is working fine:
try {
const plot = await axios.post(`${PLOT}`, plotRequestBody); //plotRequestBody is defined
However when I try to make the put request
try{
await axios.put(`${PLOT}`, plotRequestBody);
Since everything is being used in the same files I’m not sure why I get the error in the title and for the put request. Thanks for any help!