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

7
  • What do you want to log exactly? Commented May 31, 2014 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.loging it outside of it, which means it gets logged (as undefined) before it gets evaluated. Please consider reading stackoverflow.com/questions/14220321/… you're be surprised how related and helpful it is. Let me know if that helped, and again - sorry for the rude note. Commented May 31, 2014 at 20:01
  • Hi @GergoErdosi I have been trying to log my data (that is in mongo DB) into terminal. console.log(markets); Commented May 31, 2014 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. Commented May 31, 2014 at 20:03
  • @BenjaminGruenbaum I have updated the code and it seems the data is still nowhere to be logged. Updates above. Commented May 31, 2014 at 20:22

2 Answers 2

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);
        })
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

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
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);
 });

1 Comment

I tried your second suggestion and works perfectly, thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.