2017-05-03 6 views
0

これはmongo端末でクエリーを実行した後に取得するデータです db.contacts.find();ノードjs、reactjs、expressを使用して実際のWebページでmongodbからデータを取得する方法

[ { _id: 59097d937386cc3a4e7b766b, 
name: 'Oreo', 
email: '[email protected]', 
education: 'B.tech', 
address: 'West Bengal', 
mobile_no: '9120507612', 
__v: 0 }, 
{ _id: 5909814236b6663d8575fc1f, 
name: 'John', 
email: '[email protected]', 
education: 'sports', 
address: 'New Delhi', 
mobile_no: '9234567788', 
__v: 0 }, 
{ _id: 590982281eb9ab3dd5cf54b1, 
name: 'Vicky', 
email: '[email protected]', 
education: 'B.Tech', 
address: 'Burgeon', 
mobile_no: '123456789', 
__v: 0 }, 

親切

答えて

1

これはマングースを使用して行うことができます示唆しています。 Mongooseは非同期環境で動作するように設計されたMongoDBオブジェクトモデリングツールです。詳細および例については、https://www.npmjs.com/package/mongooseを参照してください。

ステップ:

var mongoose = require('mongoose'); 

接続データベースへ:

mongoose.connect('mongodb://mongo:27017/yourdatabasename'); 

定義スキーマ:

はマングースを含める

var contactSchema = mongoose.Schema({ 
_id: String, //or _id: mongoose.Schema.Types.ObjectId 
name: String, 
email: String, 
education: String, 
adress: String, 
mobile_no: Number 

})。

は、モデルの定義:

var ContactModel = mongoose.model('yourcollectionname', contactSchema); 

抽出データを、コールにウェブページに戻る:あなたのウェブページ上

app.get('/getusers', function(req, res) { 
    ConcactMOdel.find({}}, function(err, foundData) { //empty query for all data 
     if(err) { 
      console.log(err); 
      return res.status(500).send(); 
     } else { 
      return res.status(200).send(foundData); 
     } 
    }); 
}); 

を、あなたはそれを操作し、自分のデータをフェッチする要求を取得するAJAXを使用することができます。

var xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function() 
{ 
    if (this.readyState == 4 && this.status == 200) 
    { 
     var dataSet = JSON.parse(this.response); 
     //manipulate/visualise the data as you wish 
    } 
}; 
xhttp.open("GET", "/getusers", true); 
xhttp.send(); 

希望は、これは本当にあなたがまだ..but行&列ごとに表形式でUIである必要はありhelpful..thank

+0

はいに役立ちます。それをどうするか。 – GeekDeveloper

+0

@GeekDeveloperはデータの呼び出し方法に関する最新の回答を確認します。表形式のビジュアライゼーションについては、既にこれに関する多くの質問があります。 – Jan

+0

http://stackoverflow.com/questions/25373281/convert-javascript-arrays-into-html-table-using-dom – Jan

関連する問題