 mongo = require '../mongo'
 async = require 'async'
+
 exports.lastData = (req, res) ->
 #    if req.get('content-type').indexOf('application/json') == -1
 #        throw new Error("Body request is not JSON.")
 
     mongo.dataCollection.findOne(
         {'sensor_id' : req.params.sensor_id},
+        {timestamp: 1},
         (err, object) ->
             if err 
                 res.send 500

            if err 
                res.send 500
            else
                mongo.dataCollection.findOne(
                    {'sensor_id' : req.params.sensor_id},
                    (err, data) ->
                        if err 
                            res.send 500
                        else
                            sensor.lastData = data
                            res.send(JSON.stringify(sensor), 200)
                    )
        )

                    (sensor, callback) ->
                        console.log("Fetching last data")
                        console.log(sensor)
                        mongo.dataCollection.find({'sensor_id' : sensor.sensor_id}).sort({'timestamp': -1}).toArray(
                            (err, data) ->
                                if err 
                                    res.send 500
                                else
                                    sensor.lastData = data[0]
                                    console.log sensor
                                    callback null, sensor
                        )
                    (err, result) ->
                        if(err) 
                            res.send 500
