2016-05-18 14 views
-1

ctr + rをクリックすると、データ全体が表示されないことがあります。私の("/")ページに私は左側と右側を持っています。左側にはすべてのデータが表示され、右側にはランダムなデータが表示されます。ほとんどの場合は動作しますが、キーボードのctr + rをクリックすると、一部またはすべてのデータが表示されないことがあります。データがブラウザに表示されない場合があります

これは私のコードの仕組みです。

  • は(今私は3 elems。3つのドキュメントを持っている)arrからのデータにcompsコレクションを設定します。
  • 実際にはそれはコレクションからすべてのコンテンツを削除するので、「/」に行く度に3つのドキュメント(テスト用)を取得します
  • すべてのドキュメントをフィルタで検索し、左側に表示します(findメソッドがdocを返すので表示できます)、レンダリングでdocを使用します。
  • findOne()を使用してランダムを取得します。

また、私は自分のコードにconsole.log()があり、正しい情報を管理していることに注意してください。私はコンソールで3つの文書すべてを取得します。

正直言って、私はレンダリング時にモデルに複数のメソッドを使用したことはありません。モデルメソッドではランダムメソッドを使用したことはありません。だから私はおそらくそれを台無しにする。

リフレッシュ時にデータが表示されないことがある理由を知りたいと思います。

server.js

var express = require("express"), 
    app = express(), 
    mongoose = require("mongoose"), 
    ejs = require("ejs"); 
mongoose.connect("mongodb://localhost/comp1"); 
var port = process.env.PORT || 3000; 
app.set("view engine", "ejs"); 
app.use(express.static("./public")); 

var Comp = require("./models/company.js"); 

app.get("/", function(req, res){ 
    var arr = [ 
    {name : "comp1",industry : "industry1", ranking: 20}, 
    {name : "comp2",industry : "industry2", ranking: 5}, 
    {name : "comp3",industry : "industry3", ranking: 10} 
    ] 
Comp.count({}, function(err, count){ 
     var random = Math.floor(Math.random() * count); 
     Comp.find({}).remove({}, function(){ 
      console.log("removed") 
     }) 
     Comp.create(arr, function(err, docs){ 
      console.log(docs) 
      console.log(count) 
     }) 

     Comp.find({}, 'name -_id ranking', {sort :{ranking :1}}, function(err, doc){ 
      if(err) throw err; 
      Comp.findOne().skip(random).exec(function(err, result){ 
       res.render("index",{data : doc , count: random , result : result}) 
      }) 

     }) 
    }) 

}) 




app.listen(port, function(){ 
    console.log("node is listening on port : " + port); 
}) 

index.ejs

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>index</title> 
    <link rel="stylesheet" href="/style.css"> 
</head> 
<body> 
    ontop 
    <div class="container clearfix"> 
     <div class="leftSide">TEST 
      <%= data%> 
      <%data.forEach(function(comp){%> 
       <div><%= comp.name %> <span>ranking : <%= comp.ranking %></span></div> 
      <% }); %> 
     </div> 
     <div class="rightSide">side <%= count %> 
      <div><%=result %></div> 
     </div> 
    </div> 

</body> 
</html> 

company.ejs

var mongoose = require("mongoose"); 
var Schema = mongoose.Schema; 

var compSchema = new Schema({ 
    name : String, 
    industry: String, 
    ranking : Number, 
    created_at : Date, 
    updated_at : Date, 
    inc : Number 

}); 

compSchema.pre("save", function(next){ 
    var currentDate = new Date(); 
    this.updated_at = currentDate; 
    var counter = 0; 
    this.inc = counter; 
    counter++; 

    if(!this.created_at){ 
     this.created_at = currentDate; 
    } 
    next(); 
}) 

var Comp = mongoose.model("Comp", compSchema); 

module.exports = Comp; 
+0

非同期コードを同期して使用しています。これは、あなたがそれを期待する方法では決して動作しません。ちょうど複数のDBクエリを行う方法をいくつかの例を見つけると私はあなたの答えを見つけると思う – Molda

+0

約束はここに私を助けるだろうか?このリンクhttps://developer.mozilla。org/ja-ja/docs/Web/JavaScript /リファレンス/ Global_Objects/Promise?私はこれを学ばなくてはいけませんでした。 –

+0

'findOne'のようなメソッドは同期していますか?私はコールバックでそれを使用しているので、私は何かを使うべきです。 –

答えて

1

だろう最も簡単なもののあなたがやりたい方法を推奨しませんコードは以下のようになりますが、通常はコールバック地獄または運命のピラミッドとその読みにくいのでこれを使用しないでください !!!!

Comp.count({}, function(err, count){ 
    Comp.find({}).remove({}, function(){ 
     Comp.create(arr, function(err, docs){ 
     Comp.find({}, ..., function(err, doc){     
      Comp.findOne().skip(random).exec(function(err, result){ 
       res.render("index",{}) 
      })  
     }) 
     }) 
    })  
}) 

別の方法は、async.js

async.series([ 
    function(callback){ 
     Comp.count({}, function(err, count){ 
      callback(null, count); 
     }); 
    }, 
    function(callback){ 
     Comp.find({}).remove({}, function(){ 
      callback(null); 
     }); 
    }, 
    function(callback){ 
     Comp.create(arr, function(err, docs){ 
      callback(null); 
     }); 
    }, 
    function(callback){ 
     Comp.find({}, ..., function(err, doc){ 
      callback(null); 
     }); 
    }, 
    function(callback){ 
     Comp.findOne().skip(random).exec(function(err, lastResult){ 
      callback(null, lastResult); 
     }); 
    } 
], 
// optional callback, results is an array of results from each callback if any 
function(err, results){ 
    // results is now equal to [count, lastResult] 
    res.render("index",{}) 
}); 

を使用することで、最終的に私が試したか、この使用していない、そうではない100%を約束でき必ずこの

ような何か
var promise = Comp.count({}).exec(); 

promise.then(function(count) { 
    return Comp.find({}).remove({}).exec(); 
}) 
.then(function() { 
    return Comp.create(arr,).remove({}).exec(); 
}) 
.then(function() { 
    return Comp.find({}).remove({}).exec(); 
}) 
.then(function() { 
    return Comp.find({}).skip(random).exec(); 
}) 
.then(function(result) { 
    res.render("index",{}) 
}) 

マングースに関する約束の詳細はこちらHow to use mongoose Promise - mongo

+0

あなたの答えをありがとう。私は 'return Model.create(arr).exec()'について質問を書いた。それは動作しません。多分あなたはこれについていくつかの情報を持っています。 http://stackoverflow.com/questions/37309637/return-model-createarr-exec-is-not-working-in-mongoose –

+0

私はそれが問題ではないとは思わないhttp://stackoverflow.com/questions/37313905 /時にはソートされたデータが利用できないときにページをリフレッシュするとき –

関連する問題