2017-09-25 7 views
0

저는 노드 v6.11.2를 실행 중이며 MongoDB는 버전 3.4.7입니다. passportjs와 express-session을 사용하여 여러 사용자가 등록 된 MongoDB가 있습니다. displaynames, 사용자 환경 설정 등이 같은 EJS을 사용하고 같은 것들을 렌더링의 경우 :Nodejs Express 앱 - MongoDb 커서를 JSON으로 변환

user: req.user 
:

    <p> 
         <strong>id</strong>: <%= user._id %><br> 
         <strong>email</strong>: <%= user.local.email %><br> 
         <strong>password</strong>: <%= user.local.password %><br> 
         <strong>Display name</strong>: <%= user.info.displayname %><br> 
         <strong>Subjects</strong>: <%= user.info.subjects %><br> 
        </p> 

이 EJS 페이지를 렌더링 할 때, GET 요청은 다음과 같이 세션에 사용자를 참조

사용자가 로그인해야하므로 지금하고있는 일에 대해 작동하지 않습니다. 등록 된 사용자를 표시 할 수있는 페이지를 갖고 싶습니다. 표시되는 이름, 일부 기본 설정 등을 표시 할 수 있습니다. (공개 프로필과 마찬가지로). 난에 내 app.js 파일의 다음 코드 줄을 살짝 (단지 테스트 목적으로) 시도했다 : 내, 데이타베이스에있는 모든 개체를 반환이

console.log(db.users.find({})); 

내가 예상 (그것은 않습니다 내가) 내 몽고 쉘합니다 (CONSOLE.LOG없이) 실행하지만, 대신이 같은 커서를 반환 할 경우 :

Cursor { 
    _readableState: 
    ReadableState { 
    objectMode: true, 
    highWaterMark: 0, 
    buffer: BufferList { head: null, tail: null, length: 0 }, 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: null, 
    ended: false, 
    endEmitted: false, 
    reading: false, 
    sync: true, 
    needReadable: false, 
    emittedReadable: false, 
    readableListening: false, 
    resumeScheduled: false, 
    defaultEncoding: 'utf8', 
    ranOut: false, 
    awaitDrain: 0, 
    readingMore: false, 
    decoder: null, 
    encoding: null }, 
    readable: true, 
    domain: null, 
    _events: {}, 
    _eventsCount: 0, 
    _maxListeners: undefined, 
    _opts: {}, 
    _get: [Function: thunk] } 

어떻게 내가 JSON이를 변환합니까를? 미리 도움을 청하십시오.

답변

0

나는 그것을 고정 : 내 GET 요청에 몇 줄의 코드를 추가 : 대답에 대한

<% users.forEach(function (user) { %>  
    <p><%= user.info.displayname %></p> 
     <p><%= user.info.subjects %></p>          
<% }) %> 
0

db.users.find({}).toArray()toArray() 메서드를 사용할 수 있습니다. 이렇게하면 커서가 아닌 사용자 목록이 반환됩니다. 다음과 같이

+0

감사를 다음과 같이

User.find({}).exec(function(err, users) { if (err) throw err; res.render('mentors.ejs', { 'users': users }) 

내가 다음 JSON에 액세스 할 수 있었다 그러나 나는 그것을 스스로 알아 냈다. 나는 내가 질문을하기 전에 내가 충분히 이슈를 조사하지 않았다고 생각한다. 나는 내 자신의 질문에 대답 할 것이다. –