Tuesday, 4 April 2017

Querying a date range with URL params in Node/MYSQL

This has been turning me in circles for almost a week now. I have an endpoint that accepts a start date/time and an end start/time.

/data/:startdate/:enddate

I am trying to use the two params to query a range of dates from my db. Here is my full route.

router.get('/data/:startdate/:enddate', function(req, res, next) {

    pg.defaults.ssl = true;
    pg.connect(dburl, function(err, client, done) {

        if (err) throw err;
        console.log('db connected');

            //res.send(req.params.startdate + " / " + req.params.enddate); < this works fine

            client.query('SELECT * FROM table WHERE datetime >= ? and datetime <= ?',[req.params.startdate, req.params.enddate], function(err, result) {

                if(!result){
                    res.send('no result :(');
                }else{
                    res.send({ data: result.rows });
                }

                done();

            });

    });


});

I can query the table perfectly with no params so my connection is good. However, I have tried multiple variations of url params to test the query but all return as 'no result :(' Ive tried the following:

http://localhost:3000/data/03-03-2017/04-04-2017
http://localhost:3000/data/2017-03-29T15:00:00.000Z/2017-04-04T15:00:00.000Z
http://localhost:3000/data/1488517200000/1491278400000

Any ideas would be super appreciated.



via andehlu

No comments:

Post a Comment