1

hello every one i am new in node.js i am playing around with the code by building small forum where Users can write some posts . when i open the home page i expect to have every post with his owner but i get all the posts with the same user and the user change every time i refresh the page

this is my Model User

 var mongoose=require('mongoose'),


var Schema=mongoose.Schema;
var UserSchema=new Schema({
    username:String,
    email:String,
    password:String,
    insdate:{type:Date,default:Date.now},
    isDeleted:{type:Boolean,default:false},



});

var User=mongoose.model("users",UserSchema);
module.exports=User;

this is my model Post

var mongoose=require('mongoose');
var Schema=mongoose.Schema;
var PostSchema=new Schema({
    title: String,
    body: String,
    isDeleted:{type:Boolean,default:false},
    added:{type:Date,default:Date.now},
    owner:{
        type:mongoose.Schema.Types.ObjectId,
        ref:'users'
    }


});
var Post=mongoose.model("posts",PostSchema);
module.exports=Post;

and here where i try to send data to the browser i use ejs

    app.get('/', function (req, res) {
      //i want to fetch Post and User and get them in one array here //and render them to my home page like any forum
    res.render('home',{posts:posts_with_their_owners_username})

      });
});

0 Answers0