I am trying to write a basic MEAN CRUD Application but I am currently stuck on the update part of the CRUD. Below is the function I have at the moment. Can anyone help please?
router.updateJob = function(req,res) {
var job = getByValue(jobs, req.params.id);
var oldTitle = job.title;
var newTitle = req.body.title;
job.title = newTitle;
if (oldTitle !== newTitle)
res.json({message : 'Title Updated'});
else
res.json({message : 'Title not Updated '});
};
Below is the error I am getting when I try and send the new title.
<h1>Cannot read property 'title' of undefined</h1>
<h2></h2>
<pre>TypeError: Cannot read property 'title' of undefined
at router.updateJob (D:\Documents\GitHub\shyft-web-app-dev-2.0\routes\job.js:48:23)
at Layer.handle [as handle_request] (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\layer.js:95:5)
at D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:281:22
at param (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:354:14)
at param (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:365:14)
at Function.process_params (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:410:3)
at next (D:\Documents\GitHub\shyft-web-app-dev-2.0\node_modules\express\lib\router\index.js:275:10)</pre>
Finally I have added the code for the getByValue function below
function getByValue(arr, id) {
var result = arr.filter(function(o){return o.id === id;});
return result ? result[0] : null;
}
Sorry for an inconvenience.