2013-04-10 5 views
0

여기에서 초보자. Node.js 및 ExpressJS를 사용하는 응용 프로그램에서 작업하고 있습니다. 디버깅을 위해 내 콘솔에 전체 또는 부분 res.locals의 내용을 로깅하는 기본적인 필요성이 있습니다.디버깅을 위해 ExpressJS 및 Nodejs와 콘솔에 res.locals 컨텐츠를 기록하는 방법은 무엇입니까?

나는이 시도했다 : ...

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

내가 Perl's Data::Dumper의 일종 찾고 있어요,하지만 아주 기본적인 작업을해야하기 때문에 어딘가에 포인트를 놓칠 것 같다

편집 : 이것은 아마도 ExpressJS 또는 Node.js 문제는 아니지만 JS 바르 범위 문제가 아닐 수 있습니다.

+0

IF'을 console.log (res.locals.data) 예상대로 작동하지 않는 이유가 될 수 있습니다. 더 많은 코드를 볼 수 있습니까? 당신이하고있는 곳은 중요 할 수도 있습니다. – UpTheCreek

+0

@UpTheCreek, 맞습니다. 내 문제는 훨씬 더 변수 범위에 대한 것으로 보인다? 예제 코드를 편집했습니다. – smonff

+0

'foo_data'의 값을 결정하는 방법은 실제로'console.log'가 비어있는 이유이기 때문에 실제로 매우 흥미 롭다고 생각합니다. – robertklep

답변

2

console.log 대신 eyespect 모듈을 사용해보십시오. 작업하기 쉬운 방식으로 항목을 인쇄합니다.

npm install -S eyespect 

for 루프 내에 foo_data를 할당 할 때 비동기 적으로 작업하고 있습니까? 표시 다음은 미정/null이어야합니다 지적 ', 즉 일을 당신이

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)); 
+0

당신이 옳았어요! 그것은 문제를 해결합니다. – smonff

+1

아무 문제 없어, 다행 이네요. – Noah

+0

+1 질문에 대답하고 비동기적인 것에 관한 제 문제를 해결했습니다 .-D – smonff