Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb debugging - ReferenceError: console is not defined

I got js file to run within mongodb where I got console.log expression for debugging:

use test;
db.city.find().snapshot().forEach(function(city){
    var Pos = city.Pos;

    if (Pos) 
    {
        longLat = Pos.split(" ");
        console.log("longLat");  // for debugging ----------------------------  
        console.log(longLat);

        lon = longLat[0];
        lat = longLat[1];

        if (lon && lat)
        {
            lon = parseFloat(lon);
            lat = parseFloat(lat);
            longLat = [lon, lat];
            city.longLat = longLat;

        }

    }
    db.orgs.save(city);

})

When I run it...

mongo < /path/to/this/js/file.js

... I got error in the output:

ReferenceError: console is not defined

Is there any way to log intermediate results for debugging purposes?

like image 588
Maxim Yefremov Avatar asked Sep 04 '14 03:09

Maxim Yefremov


1 Answers

Use the print or printjson methods instead. These are used to emit just like console.log() within the shell:

    if (Pos) 
    {
        longLat = Pos.split(" ");
        print("longLat");  // for debugging ----------------------------  
        printjson(longLat);
like image 174
Neil Lunn Avatar answered Oct 23 '22 05:10

Neil Lunn