0

I want to console log data from a mongo db collection, That is it! So simple, but it wont work... frustrating

Here is my data: enter image description here

    //this is what i have:

    var express = require('express');
    var routes = require('./routes');
    var user = require('./routes/user');
    var http = require('http');
    var path = require('path');
    var request = require('request');
    var mongoose = require('mongoose');

// setup server
var server = http.createServer(app).listen(app.get('port'), function(){

  console.log('Express server listening on port ' + app.get('port'));
});

// setup socket
var io = require('socket.io').listen(server);

http.createServer(function(request,response){
    response.writeHead(200);
    response.write(200);
    response.end();
}).listen(8080);


// ^server setup, do your dirty down here:



var data;
// when request /
app.get('/', function(req, res) {

    // render index
    res.render('index.html');

    // get database of markets
    var markets = db.collection('ftse100');

    console.log(markets);

    markets.find(function(error,docs){
        console.log(data);
        console.log(docs);
        data = docs;
        console.log(data);
    }); 

});

This is now what i get. still no data coming through? Maybe it has something to do with the collection i am pulling from or something with MongoDB?

enter image description here

React Dev
  • 420
  • 2
  • 6
  • 16
  • What do you want to log exactly? – Gergo Erdosi May 31 '14 at 19:59
  • 1
    If I may be blunt and somewhat rude (sorry)- your problem is that you don't know how JavaScript concurrency works, not the docs or Mongo. You're setting a variable inside a callback, and probably `console.log`ing it outside of it, which means it gets logged (as undefined) before it gets evaluated. Please consider reading http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call you're be surprised how related and helpful it is. Let me know if that helped, and again - sorry for the rude note. – Benjamin Gruenbaum May 31 '14 at 20:01
  • Hi @GergoErdosi I have been trying to log my data (that is in mongo DB) into terminal. console.log(markets); – React Dev May 31 '14 at 20:01
  • @Ewan as a teaser for that post I linked you do, try to `console.log(docs)` _inside_ the function you passed to `markets.find`. – Benjamin Gruenbaum May 31 '14 at 20:03
  • @BenjaminGruenbaum I have updated the code and it seems the data is still nowhere to be logged. Updates above. – React Dev May 31 '14 at 20:22
  • What do you get when you `console.log(err)` there too? – Benjamin Gruenbaum May 31 '14 at 20:23
  • By the way, sorry all for my frustration but between learning a new language, terminal and nothing seeming to work it can all be frustrating after 8 hours of trying and getting nowhere – React Dev May 31 '14 at 20:24

2 Answers2

2

If you only want to log your document in ftse100 collection (uk db), this should be enough:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/uk');

// Wait until connection is established
mongoose.connection.on('open', function(err, doc){
    console.log("connection established");

    mongoose.connection.db.collection('ftse100', function(err, docs) {
        // Check for error
        if(err) return console.log(err);
        // Walk through the cursor
        docs.find().each(function(err, doc) {
            // Check for error
            if(err) return console.err(err);
            // Log document
            console.log(doc);
        })
    });
});
Droga Mleczna
  • 511
  • 3
  • 7
  • I can't thank you enough for this mate. Is it just me... this seems like such complicated code to get data from a database. It should be var data = db.collection('ftse100'); console.log (data); giving you the raw json in an array. Simple – React Dev Jun 01 '14 at 20:06
2

I am using Mongodb 3.6.3. There is no function find() for docs anymore;

docs.each(function(err, doc){
      if(err) return console.err(err);
            // Log document
            console.log(doc)
    });

or you can use toArray()

db.collection( 'products' ).find().toArray(function(err, docs){
    if (err) throw err;
    console.log(docs);
 });
Rafiq
  • 8,987
  • 4
  • 35
  • 35