Skip to content

Instantly share code, notes, and snippets.

@minipai
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minipai/11309560 to your computer and use it in GitHub Desktop.
Save minipai/11309560 to your computer and use it in GitHub Desktop.
var mongoose = require( 'mongoose' )
, ObjectId = mongoose.Schema.Types.ObjectId
, Board = mongoose.model( 'Board' )
, BoardAdmin = mongoose.model( 'BoardAdmin' )
, Post = mongoose.model( 'Post' )
exports.show = function(req, res) {
var data = {}
Board
.findOne({name: req.params.board})
.exec()
.then(function(board) {
data.board = board
return BoardAdmin.findOne({_board: board._id, _user:req.user._id}).exec()
})
.then(function(boardAdmin) {
return Post
.find( {_board: boardAdmin._name} )
.where('_id').nin(boardAdmin.hidden_posts)
.exec()
})
.then(function(posts) {
data.post = posts
res.render('content/boards/show', data)
})
.then(null, function(err) {
console.log(err)
})
.end()
}
var boardSchema = new Schema({
name : { type: String, required: true, trim: true },
title : { type: String, required: true, trim: true },
post_count : { type: Number, default: 0 },
updated_at : { type: Date, default: new Date() },
master : { type: ObjectId }
});
var boardAdminSchema = new Schema({
_user : { type: ObjectId , ref: 'User' },
_board : { type: ObjectId , required: true, ref: 'Board' },
stared_posts : [ { type: ObjectId, ref: 'Post' } ],
hidden_posts : [ { type: ObjectId, ref: 'Post' } ],
blocked_users : [ { type: ObjectId, ref: 'Post' } ],
});
var postSchema = new Schema({
title : { type: String , required: true, trim: true },
content : { type: String , required: true, trim: true },
status : { type: String , trim: true },
embed : { type: Object },
comments : { type: Array, },
_board : { type: String , required: true, ref: 'User' },
_user : { type: ObjectId , required: true, ref: 'User' },
_replyTo : { type: ObjectId , ref: 'Post' },
_crossForm: { type: ObjectId , ref: 'Post' },
updated_at: { type: Date, default: new Date() }
});
@tomchentw
Copy link

I think u can merge two then calls into one:

      .then(function(posts) {
        data.post = posts
        res.render('content/boards/show', data)
      }, function(err) {
        console.log(err)
      })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment