2017-07-29 46 views
0

我想呈現主要是mongoDB數據和數組。我想要做的就是讓我的ejs文件使用數據庫中的組件。 這裏是我的代碼:處理ejs文件的mongodb數據的最佳方法

//Array to store all restaurants 
var restArray = []; 

//Need database restaurant schema 
var databaseArray = []; 

//Find the restaurant in the database, 
//If not there create one. 
var findOrCreate = function() { 
    Restaurant.findOne({ 
      nameOfRest: this.nameOfRest 
     }) 
     .then(exist => { 
      if (!exist) { 

       this.save() 
        .then(result => { 
         databaseArray.push(result); 
        }) 
        .catch(err => { 
         console.log(err); 
        }) 
      } else { 
       databaseArray.push(exist); 
      } 
     }) 
     .catch(err => { 
      debugger; 
      console.log(err); 
     }) 
} 
//Go through each restaurant and put in function. 
restArray.forEach(function(restArr) { 

    var tempRest = new Restaurant({ 
     nameOfRest: restArr.restaurant.name, 
     favoriteFoods: [], 
    }); 
    //databaseArray.push(tempRest); 

    findOrCreate.call(tempRest); 
}); 
    //*****************I would use database array here 
res.render('restaurant', { 
    restHTML: restArray 
}); 

這是不正確的,它不是全部推到數據庫陣列在時間,因爲代碼是異步的。我的問題是有更好的方式來訪問ejs文件中使用的數據庫數據,還是必須找到一種方法將模式數據推送到數據庫陣列中,並將其與restArray數據並行顯示。 (我需要restArray)。我想是這樣chart.js之這是在我的EJS此文件內使用MongoDB的數據:使用ECMAScript 6臺發電機

<script> 
window.onload = function() 
{ 
    var ctx = document.getElementById("myChart").getContext('2d'); 
    var myChart = new Chart(ctx, 
    { 
     type: 'bar', 
    data: { 
     labels: ["Chili dog", "Burger", "Zucchini Fries"], //*****************Put mongodb data inside here ******************* 
     datasets: [{ 
      label: 'Best food here:' , 
      data: [12, 19, 3], //********************Put mongodb data inside here. ****************** 
      backgroundColor: [ 
       'rgba(255, 99, 132, 0.2)', 
       'rgba(54, 162, 235, 0.2)', 
       'rgba(255, 206, 86, 0.2)', 
      ], 
      borderColor: [ 
       'rgba(255,99,132,1)', 
       'rgba(54, 162, 235, 1)', 
       'rgba(255, 206, 86, 1)', 
      ], 
      borderWidth: 1 
     }] 
    }, 
    options: { 
     responsive: false, 
     scales: { 
      yAxes: [{ 
       ticks: { 
        beginAtZero:true 
       } 
      }] 
     } 

    } 
    }); 
} 
</script> 
+0

你說你需要的databaseArray,但我沒有看到它被任何地方使用。它究竟用於什麼? – Paul

+0

我很可能結合restArray和databaseArray在res.render中使用它。我並不確定如何解決這些問題,因爲我是新來的node.js @Paul –

+0

不,我的意思是你需要什麼。 – Paul

回答

1

開始。所以你的代碼看起來是同步的,Generators + co爲你做了異步魔法。瞭解更多關於在這裏http://mongoua.tk/presentations/ecmascript-6-and-the-node-driver

搭配發電機+ Co的代碼看起來像這樣

var co = require('co'); 

var databaseArray = []; 
co(function*() { 
    var record = yeild Restaurant.findOne({nameOfRest:this.nameOfRest}); 
    if(!record){ 
    record = yeild Restaurant.save(this); 
    } 
    databaseArray.push(record); 
    // do other operations.... 
    res.render('restaurant',{restHTML:restArray}); 
}).catch(function(err) { 
    console.log(err.stack); 
    res.status(500).send(err); 
}); 
+0

如果你打算推薦他用'讓你的代碼看上去同步'的路線(我不同意但是無論如何),爲什麼不去與異步/等待的方法,而不是添加另一個第三方依賴? – Paul