1

I'm working on an application using Node.js and ExpressJS and I'm a beginner on this environment. I have the basic need of logging the entire or partial res.locals's content in my console for debugging.

I've tried this :

var foo_route = function(req, res){

    var foo_data = [];

    for (var i = 0; i < data.response.foo.length; i++) {

        // Here complex data management, not interesting in our case
        foo_data = // Bla bla ...

        // OK. This displays my object content
        console.log(foo_data);

    }

    // Seems because my misunderstanding of JS scopes, foo_data is still == []
    res.locals.data = foo_data;

    // Nothing. Both of these two solutions display nothing
    console.log(res.locals.data);
    console.log(JSON.stringify(res.locals.data, null, 2));

I'm searching for a kind of Perl's Data::Dumper, but it seems that I miss the point somewhere because it should be a very basic task...

Edit : I know this is maybe not specifically an ExpressJS or Node.js problem but much more a JS vars scope problem...

Note for people who would read this in 2018: don't use var, use let and const.

smonff
  • 3,399
  • 3
  • 36
  • 46
  • IF `console.log(res.locals.data);` displays noting then it must be null/undefined. Can we see more code? Where you are doing it might be important. – UpTheCreek Apr 10 '13 at 11:34
  • @UpTheCreek, yes you are right. My problem seems to be much more about variables scope isn't it ? I've edited the example code. – smonff Apr 10 '13 at 11:55
  • I'm thinking the way you determine the value of `foo_data` is actually very interesting, given that it's probably the reason your `console.log`'s are empty. – robertklep Apr 10 '13 at 12:57

1 Answers1

2

Try using the eyespect module instead of console.log. It prints things in a way that is easier to work with.

npm install -S eyespect

When you assign foo_data inside the for loop, are you doing anything asynchronously? That could be a reason why things are not working as you expect

var foo_route = function(req, res){

    var foo_data = [];
    // what is data here? Also var i should technically be declared at the top of the function
    for (var i = 0; i < data.response.foo.length; i++) {

        // Here complex data management, not interesting in our case
        foo_data = ...

        // OK. This displays my object content
        console.log(foo_data);

    }
    // does this print anything interesting?
    inspect(foo_data, 'foo data outside the loop')
    inspect(res.locals, 'res.locals')
    // Seems because my misunderstanding of JS scopes, foo_data is still == []
    res.locals.data = foo_data;
    inspect(res.locals.data, 'res.locals.data')    

    // Nothing. Both of these two solutions display nothing
    console.log(res.locals.data);
    console.log(JSON.stringify(res.locals.data, null, 2));
Noah
  • 33,851
  • 5
  • 37
  • 32
  • Full disclosure, eyespect is a fork of eyes.js https://github.com/cloudhead/eyes.js but with different default colors. – Noah Apr 10 '13 at 20:31
  • I've tried your Eyespect module, it's an amazing piece of software, quite the same than the Perl's Data::Printer (colored Data::Dumper). We will use it at work, it will be sweet for our eyes when debugging ! – smonff Apr 10 '13 at 20:52