2017-12-29 1 views
0

MongoDB에서 정보를 표시하는 아주 간단한 응용 프로그램을 구축하고 있습니다 (몽구스 사용).Node.js, Express 및 Handlebars를 사용하여 탐색 모음에 MongoDB 문서의 필드 값을 표시합니다.

내비게이션 막대에서 문서의 필드 값을 started으로 표시하고 싶습니다. 그래서 모든 단일 페이지에 표시되어야합니다.

{ 
    "_id" : ObjectId("5a4668271f12a77878e97bf1"), 
    "started" : 1514563623, 
    "discipline" : "coding", 
    "isCompleted" : false, 
    "date" : ISODate("2017-12-29T16:07:03.340Z"), 
    "__v" : 0 
} 

나는 핸들 바 헬퍼 (Handlebars Helper) 내부에서 함수를 만들려고 시도했다. 그러나 나는 보통 같은 문제를 겪는다 : 나는 console.log 할 수있다. 그러나 나는 return 할 수 없다.

또한 미들웨어를 만들려고했습니다. 그리고 내 localhost 환경에서 다소 효과가 있지만, 프로덕션 환경으로 옮길 때 작동하지 않습니다. 네비게이션 바 핸들에 전화

var currentLogNavBar = function (req, res, next) { 

    // Load Model 
    require('./models/Log'); 
    const Log = mongoose.model('logs'); 

    Log.findOne({ 
     isCompleted: false 
    }) 
    .then(logs => { 
     res.locals.lastLogSecondsStarted = logs.started || null; 
    }); 
    next() 
    } 

app.use(currentLogNavBar) 

: 그리고

{{{lastLogSecondsStarted}}} 

내가 문제를 생각은 node.js.의 비동기 자연과 함께 할 수있는 뭔가가

다른 접근 방법을 사용해야합니까? 내가 뭘 잘못하고 어떻게 작동시킬 수 있니?

감사합니다.

편집 : MongoDB 문서의 예가 추가되었습니다.

var currentLogNavBar = function (req, res, next) { 

    // Load Model 
    require('./models/Log'); 
    const Log = mongoose.model('logs'); 

    Log.findOne({ 
     isCompleted: false 
    }) 
    .then(logs => { 
     res.locals.lastLogSecondsStarted = logs.started || null; 
     next(); 
    }) 
    .catch(err => { 
     next(err); 
    }); 

app.use(currentLogNavBar) 
:

답변

0

내 문제를 해결
관련 문제