여기에서 초보자. 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 바르 범위 문제가 아닐 수 있습니다.
IF'을 console.log (res.locals.data) 예상대로 작동하지 않는 이유가 될 수 있습니다. 더 많은 코드를 볼 수 있습니까? 당신이하고있는 곳은 중요 할 수도 있습니다. – UpTheCreek
@UpTheCreek, 맞습니다. 내 문제는 훨씬 더 변수 범위에 대한 것으로 보인다? 예제 코드를 편집했습니다. – smonff
'foo_data'의 값을 결정하는 방법은 실제로'console.log'가 비어있는 이유이기 때문에 실제로 매우 흥미 롭다고 생각합니다. – robertklep