0

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 &#39;title&#39; of undefined</h1>
<h2></h2>
<pre>TypeError: Cannot read property &#39;title&#39; 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.

  • Where are you stuck ? What is the problem ? – Brahma Dev Oct 22 '17 at 09:57
  • Hi sorry, this is the error I am receiving when I try to send the update. – Cj O Sullivan Oct 22 '17 at 09:59
  • Perhaps start by [reading some documentation](https://docs.mongodb.com/manual/crud/). And then maybe search for answers of those who asked before. Your're not the first. – Neil Lunn Oct 22 '17 at 10:00
  • 1
    You might also grasp that `var job = getByValue(jobs, req.params.id);` does not tell anyone what `getByValue()` is supposed to do, since you don't include that in your question. It's also worth noting that anything with the database is going to be an "async" call. So you probably should be reading: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Neil Lunn Oct 22 '17 at 10:02
  • Updated the problems with the code. – Cj O Sullivan Oct 22 '17 at 10:09

3 Answers3

0

Try this:

function getByValue(arr, id) {
  var result = arr.filter(function(o){return o.id.toString() === id.toString();});
  return result ? result[0] : null;
}
Ayush Mittal
  • 539
  • 2
  • 5
0

The error that you are getting is most likely caused by job returning undefined. As for why that happens, I can't be sure. Add a check to see if both the job and the title exist to catch the error. The answer provided by @Ayush may be the solution to fix your getByValue() function so that it returns the correct data, depending on the format of req.params.id and the id key of the job.

    router.updateJob = function(req,res) {

        var job = getByValue(jobs, req.params.id);
        if(!job || !job.title){
            return res.json({message: 'error while fetching item'})
        }
        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 '});
};
JoeWemyss
  • 607
  • 1
  • 10
  • 28
0

You are not defining the array jobs before using it here

var job = getByValue(jobs, req.params.id);

jobs is undefined making the variable job undefined which gives you the error

TypeError: Cannot read property &#39;title&#39; of undefined

(unless you are defining it somewhere else, which is unlikely).

You can either you use the debugger or insert a console.log(jobs); right above the var job line to see the content of jobs.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50