Learnyoumongo insert tutorial - console.log not working

Hello,

I am on lesson #5 of this tutorial and console.log isn’t printing anything. I found threads where people had this problem and the solution was to refresh the browser, but this doesn’t help in my case. Could it be a c9 issue?

Here is my code:

console.log(‘running’);
var mongo = require(‘mongodb’).MongoClient;
var url = ‘mongodb://localhost:27017/learnyoumongo’;
var first = process.argv[2];
var last = process.argv[3];
mongo.connect(url, function(err, db){
if (err){
console.log(‘error’);
throw err;
}
var docs = db.collection(‘docs’);
docs.insert({
‘firstName’:first
,‘lastName’:last
}, function(err, data){
if (err){
console.log(‘error’);
throw err;
}
console.log(JSON.parse(data));
db.close;
})
})

When I run it in bash:

armytank:~/workspace $ learnyoumongo run insert.js
armytank:~/workspace $

The first line should print something but it doesn’t. Does anyone have any suggestions? Thanks so much.

I should mention that when I run the file with ‘node insert.js’ then it does print ‘running’, so I think this is a problem with learnyounode.

Your data variable is an object containing result of the insertion, not a string, so you don’t need to JSON.parse it.

I appreciate the feedback but I don’t think that is the main issue here. The log command in the first line should definitely be printing something but it isn’t.

I tried this lesson and learnyoumongo run also didn’t print anything. However, you can use learnyoumongo verify to pass and get to the next lesson or run it with node as you did to see it working. And don’t forget parentheses after db.close

Thanks, I figured it out by running through learnyoumongo verify.

1 Like

i’m having the same issue as well with console.log not working. Mine also doesn’t verify, my code may not be identical to yours, but it’s very similar

I noticed this same thing. I found this thread on github about the issue and the creator of the learnyoumongo module responded with:

“Yes, this exercise purposefully compares stdout of both your script and one executing the expected way. Closing as this is by design. (If you have a better way of doing this, I am all ears :])”

It sounds like not letting us use console.log for this exercise was intended, but I don’t really understand why. It specifically says in the instructions “Use console.log to print out the object used to create the document.” But if we can’t actually see what is coming through to stdout, all we can do is use the learnyoumongo verify method to check our work. It sure makes debugging a pain but I guess it’s doable.