Node.js and Express - TypeError: Can not read property

1

I'm getting the following error in the console: TypeError: Can not read property '_id' of undefined

I'm reading the book "Mean Full Stack Javascript ..." from the code house. During the development of the application presented in the book, I got the error above. What I am trying to do is to save or update a given data in a static list on the server. However, after passing the AngularJs (routes and controller) and arriving in Express the controller receives the object sent as undefined.

Giving a searched, some places indicated gives a look at the body-parser, but here everything is ok, based on the book and what the guys advised on other pots. However, I could not solve the problem. Here are the codes below:

HTML

<form ng-submit="salvar()" >
<div class="form-group">
    <label for="nome">Nome Completo</label>
    <input class="form-control" type="text" id="nome" name="nome" required ng-model="contato.nome">
</div>
<div class="form-group">
    <label for="email">Email</label>
    <div class="input-group">
        <span class="input-group-addon">@</span>
        <input class="form-control" type="text" id="email" name="email" required ng-model="contato.email">
    </div>
</div>
<div>
    <button type="submit" class="btn btn-primary">Salvar</button>
    <a href="#/" class="btn btn-default">Voltar</a>
</div>

Controller AngularJS

var Contato = $resource('/contatos/:id');

        $scope.salvar = function(){
        $scope.contato.$save()
            .then(function(){
                $scope.mensagem = "Contato salvo com sucesso!";
                $scope.contato = new Contato();
            })
            .catch(function(erro){
                console.log(erro);
            });
    };

Express Route

var controller = app.controllers.contatoController;

    app.route('/contatos')
        .get(controller.listaDeContatos)
        .post(controller.salvarContato);

    app.route('/contatos/:id')
        .get(controller.obtemContatoPorId)
        .delete(controller.remover)
        .post(controller.salvarContato);

Controller Express

    controller.salvarContato = function(req, res){
    console.log("Chegou até salvarContato!");
    var contato = req.body;
    console.log(contato);
    contato = contato._id ? atualiza(contato) : adiciona(contato);
    res.json(contato);
}

In short. The object arrives in the undefined express ... I have already reviewed the part of the book that explains about, I already searched the net, until I gave up and came here. rsrs

Any help will be welcome!

In order not to be left out, I also leave the body-parser configuration .

ps: the required module name is the same as the dependency in the package.json file

var bodyParser = require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));

Vlw!

    
asked by anonymous 30.07.2016 / 06:49

1 answer

2

I was able to fix it. The problem was in the express configuration file.

I was first calling express-load to import the files before setting the body-parser. I just reversed the order of the calls and the request worked.

The error in fact was that the object was not arriving at the server because the body-parser is not able at the time needed to do the conversion from Json to object and vice versa.

I hope I have helped someone with my problem!

    
02.08.2016 / 00:34