我正在建立一个网站,我只希望一个人在关注社区时才能进入页面。
我试图为“跟随”创建一个模式,其中包括用户名和要遵循的社区。一旦这样做,我就将社区ID和用户名放入数据库,但是在让他们进入页面之前,我无法弄清楚如何检查他们是否在关注。
var communitySchema = new mongoose.Schema ({
name : String,
branches : [branchSchema]
});
var Community = mongoose.model("Community", communitySchema);
var followSchema = new mongoose.Schema ({
user : String, //going to use the current user username
community : String //going to use the current community id
});
var Follow = mongoose.model("Follow", followSchema);
//community page
app.get('/community/:id', isLoggedIn, function(req, res){
Community.findById(req.params.id).populate('Branch').exec(function(err,
foundcommunity){
if(err){
console.log(err);
}else{
res.render('community.ejs', {community:foundcommunity});
}
});
});
//following system
app.post('/community/:id/follow', function(req, res){
var user = req.user.username; //to get current user
var community = req.params.id; // to get the community id
var follow = {user: user, community: community};
Follow.create(follow, function(err, user){
if(err){
console.log(err);
}
res.redirect('/')
});
});
预期结果是网站将在打开社区页面之前检查用户是否关注。