When i click ctr + r
sometimes the whole data doesn't show up. on my ("/")
page I have a left side and a right side. The left side should display all of the data and the right side should display a random piece of data. It works most of the time but if I click ctr + r
on the keyboard to refresh sometimes some or all the data doesn't appear .
This is how my code works.
- sets the
comps
collections to the data fromarr
(now I have 3 elems. 3 docs). - Actually before it does that it deletes all the content from the collection so every time I go to "/" I get only 3 docs (for testing)
- find all docs with the filters and display them on the left side (I can display them because the find method returns doc) then I use doc in the render.
- use the findOne() to get the random.
Also you should note that I do have the console.log() in my code and that consoles the correct information. I get all 3 documents in the console.
To be honest I never used more than one method on the model when rendering and I never used a random method in a model method. so I maybe messing that up.
I want to know why sometimes the data doesn't appear when I refresh.
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;