Happy Thanksgiving to all! I hope everyone enjoys the delicious food on their plates. I know I definitely will!
Anyway,
I'm currently attempting to query my mongoDB collection for the latest inserted document and send it to the client. However, instead of seeing the data from the document on the webpage, all that appears is [object Object]. When I log the sent data, this is what shows up:
Object {newData: Array[1]}
newData: Array[1]
0: Object_id: "564f8b8cfa74e4daab1543ca"
ambientAtTrackTempSensor: "-"
date: "10/09/2015"
dewPoint: "56"
humidity: "63.0"
pressure: "29.258"
solar: "397"
temp: "80.1"
time: "17:00"
trackTemp: "-"
windBearing: "222"
windGust: "7.0"
windSpeed: "6.0"
__proto__: Object
length: 1
__proto__: Array[0]
__proto__: Object
The data is there, so I believe I'm just not accessing it correctly.
Here is the code snippet:
Server Side:
var io = require('socket.io').listen(server);
MongoClient.connect(dbUrl, function(err, db) {
if (err) {
console.log('Failed to connect to mongoDB database.');
} else {
console.log('Successfully connected to mongoDB database.');
};
io.sockets.on('connection', function(socket) {
// sending data to the client
var collection = db.collection('track_weather');
collection.find().sort({_id:-1}).limit(1).toArray(function(err, docs) {
console.log(docs);
socket.emit('message', {'newData': docs});
});
});
});
Client Side:
<body>
<script>
var socket = io.connect();
socket.on('message', function(data) {
$('#newData').text(data.newData);
console.log(data);
});
</script>
<div id="newData"></div>
</body>
Any assistance would be much appreciated! I'm still learning the ropes in this field :)