Wednesday, 17 May 2017

node.js nested http req object undefined - bodyParser.urlencoded({ extended: true } not working

why is the nested object

kategorien : [Object]

undefined in my node.js app?

Here is the console log from the console.log in the katalogCtrl:

     { _id: '591c08ccb673741787b123c2',
  bezeichnung: 'test',
  __v: 0,
  risiken: [ { bezeichnung: 'Naturgefahren', kategorien: [Object] } ] }

the server.js:

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, application/json');
    res.setHeader('Access-Control-Allow-Credentials', true);
    res.setHeader("Content-Type", "application/json");
    next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

router.route('/katalog/update').post(
    authstrategies.authJWToken,
    authstrategies.check_if_is_admin,
    katalogCtrl.update_katalog);

the katalogCtrl:

exports.update_katalog = function (req, res) {
    console.log(req.body);
}

and here the call from the client (angular 2):

    let headers = new Headers({ 'Content-Type': 'application/json' });
          headers.append('Authorization', localStorage.getItem('token'));
          let options = new RequestOptions({ headers: headers });

let body = JSON.stringify(katalog);
            console.log(body);
            this.http
                .post(this.CONSTANTS.APIURL+'/katalog/update', body, options)
                .toPromise().
            then(value => {
                console.log(value);
                })
                .catch();

the console.log from clients side:

    {
    "_id": "591c08ccb673741787b123c2",
    "bezeichnung": "test",
    "__v": 0,
    "risiken": [{
        "bezeichnung": "Naturgefahren",
        "kategorien": [{
            "_id": "5915ab9d19d0d027b870d259",
            "beschreibung": "-",
            "name": "Hochwasser"
        }, {
            "_id": "5915abd019d0d027b870d25a",
            "beschreibung": "-",
            "name": "Muren"
        }, {
            "_id": "5915abd719d0d027b870d25b",
            "beschreibung": "-",
            "name": "Oberflächenwasser"
        }]
    }]
}



via miholzi

No comments:

Post a Comment