Friday, 21 April 2017

Express 4 POST - req.res.some_property is undefined

I have looked over every recommended stack posting, the body-parser NPM documentation and various web blogs and can't resolve my issue

When I send a POST request from my Angular 4 app to my Node.JS / Express / MongoDB server req.body.data is undefined

Data being POSTed:

data = [{"startTime":"15:30", "endTime":"15:45"}]

My Angular Method:

getRange(data: object): Observable <Dataset[]> {
    console.log( "data being sent " + JSON.stringify(data) );
    let headers = new Headers({'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    const url = 'http://localhost:3039/read/filterget';

    return this.http.post(url, data, options)
        .map(this.extractData )
        .catch(this.handleError);
}

My Express App

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

var http = require('http');
var io = require('socket.io')(http);
var cors = require('cors');


var path = require("path");
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL, use the project name
var url = 'mongodb://localhost:27017/clientserverExpSockIO';

var app = express();

// npm bodyParser/Express setup https://www.npmjs.com/package/body-parser
app.use(bodyParser.json({ type: 'applictaion/json' }) );
app.use(bodyParser.urlencoded({ extended: false })); 
var jsonParser = bodyParser.json();

app.set('view engine', 'ejs')



app.post('/read/filterget', jsonParser, function (req, res) {

    console.log(req.headers);  
    if (!req.body) return res.sendStatus(400);

    console.log(JSON.stringify(req.body.data) );

    var params = req.body.data

    if (req.body.data)
    {
        console.log('data' + JSON.parse(data));
        params = JSON.parse(req.body.data);
    } else { console.log("params not set"); }


    MongoClient.connect(url, function (err, db) {
        assert.equal(null, err);
        console.log('mongodb connection opened');
        var collection = db.collection('json');

        if (params.startTime && params.endTime) {
            collection.find({ time: { $gt: params.startTime, $lt: 
        params.endTime } }).toArray(function (err, docs) {
                assert.equal(err, null);


                res.setHeader('Content-Type', 'application/json');
                res.send(JSON.stringify(docs) /*, null, '\t'*/);

                // console.log("Found the following records");
                // console.log(docs)
                db.close();
            });
        } else {
            console.log('Start & End time not available');
            collection.find({}).toArray(function (err, docs) {
                assert.equal(err, null);
                res.setHeader('Content-Type', 'application/json');
                res.send(JSON.stringify(docs) /*, null, '\t'*/);
                db.close();
            });

        }
    });

});

The output of console.log(req.headers) when the POST api is consumed:

headers output



via Jake

No comments:

Post a Comment