2017-08-30 135 views
1

我有一些MongoDB集合,並且需要在我的站點中顯示它們,使它成爲一個動態頁面(它是團隊成員,如果刪除或添加1要刷新頁面上)NodeJS從MongoDB中檢索多個數據並在Jade上顯示(Pug)

我的收藏是像(mongodb的終端):

db.capitao.find() = { "_id" : ObjectId("59a6b67519404d41f9988524"), "nome" : "Renato", "email" : "[email protected] 
om", "imagem" : "renato.png", "curso" : "Engenharia Mecânica", "lider" : 1 } 

db.chassi.find() = { 
{ "_id" : ObjectId("59a6b69f19404d41f9988529"), "nome" : "Carlos", "email" : "[email protected] 
", "imagem" : "possebon.png", "curso" : "Engenharia Mecânica", "lider" : 1 } 
{ "_id" : ObjectId("59a6b69f19404d41f998852a"), "nome" : "Felipe", "email" : "[email protected] 
l.com", "imagem" : "brunetto.png", "curso" : "Engenharia Mecânica", "lider" : 0 } 
{ "_id" : ObjectId("59a6b69f19404d41f998852b"), "nome" : "Isabelle", "email" : "[email protected] 
gmail.com", "imagem" : "isabelle.png", "curso" : "Engenharia Mecânica", "lider" : 0 } 
{ "_id" : ObjectId("59a6b69f19404d41f998852c"), "nome" : "Mateus Dandolini Pescador", "email" : "[email protected] 
r", "imagem" : "pescador.png", "curso" : "Engenharia Mecânica", "lider" : 0 } 
{ "_id" : ObjectId("59a6b69f19404d41f998852d"), "nome" : "Marcelino Colla Junior", "email" : "[email protected]", 
"imagem" : "marcelino.png", "curso" : "Engenharia Mecânica", "lider" : 0 } 

我index.js(僅試圖從 「capitao」 檢索在前):

router.get('/', function(req, res,next) { 
    var resultArray = { 
         capitao : [], 
         chassi : [] 
        }; 

    var db = req.db; 

    //var collectionCapitao = db.collection('capitao').find(); 

    var collectionCapitao = db.get('capitao'); 

    //var chassi = db.collection('chassi').find(); 

    collectionCapitao.find({},{},function(e,docs){ 
     res.render('index', { 
      env: env, 
      capitao: collectionCapitao 
     }); 
    }); 
}); 

我index.jade:

  .row 
       each membro, i in capitao 
        .col-sm-4 
         .team-member 
          img.mx-auto.rounded-circle(src='/img/team/#{membro.imagem}', alt='#{membro.nome}') 
          h4 membro.nome 
          p.text-muted membro.curso 
          ul.list-inline.social-buttons 
           li.list-inline-item 
            a(href="mailto:#{membro.email}") 
             i.fa.fa-envelope 

我的問題:

  • 所有屬性在玉返回undefined(解決)
  • 「capitao」 只有1分的記錄,但它產生的31條記錄翡翠(已解決)
  • 如何獲取index.js中的MULTIPLE集合併發送它們做Jade?
+1

您不應該發佈卡洛斯,菲利普和伊莎貝爾的電子郵件地址。 –

+0

它應該是'res.render( '索引',{ ENV:ENV, capitao:文檔 });','不capitao:collectionCapitao' –

+0

固定,@JeremyThille – alvarosps

回答

0

我剛剛改寫您已發佈的解決方案(很高興它的工作原理:)因爲你重複自己很多(10次同樣的功能!)

所以這裏是DRY版本(不要重複你自己)。 免責聲明:這是未經測試的代碼!

router.get('/', function (req, res, next) { 

    const db = req.db; 
    const async = require("async") 

    const names = ['capitao','aerodinamica','chassi','controles','drivetrain','eletronica','gestao','marketing','powertrain','suspensao'] 

    const collections = names.map(name => db.get(name)) 

    const functions = collections.map(collection => { 
     return done => collection.find({}, done) 
    }) 

    async.series(functions, (err, results) => { 
     // "results" is now an array containing [ docs1, docs2, .. ] 
     res.render('index', { 
      env: env, 
      capitao: results[0], 
      aerodinamica: results[1], 
      chassi: results[2], 
      controles: results[3], 
      drivetrain: results[4], 
      eletronica: results[5], 
      gestao: results[6], 
      marketing: results[7], 
      powertrain: results[8], 
      suspensao: results[9] 
     }); 
    }) 
}); 
+0

非常感謝@Jeremy Thille,我實際上是Javascript/NodeJS的新手。有趣的是,我們可以做同樣的事情,我會把它放在我的項目上:D,我會有其他的集合訪問,所以它會真的到手:D(和代碼作品^^) – alvarosps

+0

很酷,那麼我會很感激,如果你upvoted和/或驗證我的答案,所以我可以得到一些代表。謝謝! –

+0

已驗證!我不能因爲低代表而不滿意。 – alvarosps

0

它看起來像你必須多次調用你的數據庫並等待所有的結果,然後將所有這些結果一起傳遞到你的模板。以下是如何與AsyncJS做到這一點:

const async = require("async") 

async.series([ 
    done => collectionCapitao.find({}, done), 
    done => collectionChassi.find({}, done) 
], (err, results) => { 
    // "results" is now an array containing [ docs1, docs2 ] 
    res.render('index', { 
     env: env, 
     capitao: results[0], 
     chassi : results[1] 
    }); 
}) 

由於這個代碼是相當小而簡潔,這裏有一個稍微更詳細的版本,以便您更好地瞭解發生了什麼:

const getCapitao = done => { 
    collectionCapitao.find({}, (err,docs) => { 
      done(err, docs) 
    }) 
} 

const getChassi = done => { 
    collectionChassi.find({}, (err,docs) => { 
      done(err, docs) 
    }) 
} 

async.series([ 
    done => getCapitao(done), 
    done => getChassi(done) 
], ...... // get both results here 
+0

它工作@Jeremy Thille,但有時我得到一些錯誤,不會加載頁面..(節點:5196)UnhandledPromiseRejectionWarning:未處理的承諾拒絕(拒絕ID:1):錯誤:回調已經調用了 。 (node:5196)[DEP0018]棄用警告:未處理的承諾拒絕已棄用。未來,未處理的承諾拒絕 將終止節點。js使用非零退出代碼進行處理。 – alvarosps

0

這裏的代碼現在,解決

router.get('/', function(req, res,next) { 

    var db = req.db; 


    var collectionCapitao = db.get('capitao'); 
    var collectionAerodinamica = db.get('aerodinamica'); 
    var collectionChassi = db.get('chassi'); 
    var collectionControles = db.get('controles'); 
    var collectionDrivetrain = db.get('drivetrain'); 
    var collectionEletronica = db.get('eletronica'); 
    var collectionGestao = db.get('gestao'); 
    var collectionMarketing = db.get('marketing'); 
    var collectionPowertrain = db.get('powertrain'); 
    var collectionSuspensao = db.get('suspensao'); 


    const async = require("async") 

    const getCapitao = done => { 
     collectionCapitao.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getAerodinamica = done => { 
     collectionAerodinamica.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getChassi = done => { 
     collectionChassi.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getControles = done => { 
     collectionControles.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getDrivetrain = done => { 
     collectionDrivetrain.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getEletronica = done => { 
     collectionEletronica.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getGestao = done => { 
     collectionGestao.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getMarketing = done => { 
     collectionMarketing.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getPowertrain = done => { 
     collectionPowertrain.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    const getSuspensao = done => { 
     collectionSuspensao.find({}, (err,docs) => { 
       done(err, docs) 
     }) 
    } 

    async.series([ 
     done => getCapitao(done), 
     done => getAerodinamica(done), 
     done => getChassi(done), 
     done => getControles(done), 
     done => getDrivetrain(done), 
     done => getEletronica(done), 
     done => getGestao(done), 
     done => getMarketing(done), 
     done => getPowertrain(done), 
     done => getSuspensao(done), 
    ], (err, results) => { 
     // "results" is now an array containing [ docs1, docs2, .. ] 
     res.render('index', { 
      env: env, 
      capitao: results[0], 
      aerodinamica : results[1], 
      chassi : results[2], 
      controles : results[3], 
      drivetrain : results[4], 
      eletronica : results[5], 
      gestao : results[6], 
      marketing : results[7], 
      powertrain : results[8], 
      suspensao : results[9] 
     }); 
    }) 
}); 
相關問題