Angular / Node / Mongoose - 渲染帖子,评论,回复

时间:2016-04-18 04:24:34

标签: javascript angularjs node.js mongodb mongoose

我正在创建一个人们可以提问的应用程序。回答并回答答案。我可以渲染帖子和答案没有问题,但无法回答答案。有人可以给我提示最佳方法吗?

html page

    <div class="container-fluid" id='post_container' ng-repeat='post in vm.posts' style='border: solid'>
    <div class="row">
    <div class = 'container'>
        <div class='single_post'>
            <h3>Topic: {{post.topic}}</h3>
            <h5>Posted By: {{post.owner}} </h5>
            <p>Description: {{post.description}}</p>
            <form class="form-horizontal">
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">Answer</label>
            <div class="col-sm-10">
                <textarea class="form-control" rows="3" ng-model='vm.newAnswer[$index]'></textarea>
            <button class='btn btn-primary' ng-click = 'vm.Answer(post._id, $index)' style='float:right'>Submit</button>
            </div>
         </form>
     </div>
     <em>Answers</em>
     <!-- Make API Call to get answers into the container change varialbe -->
     <div class='answer_container' ng-repeat = 'answer in post.answers track by $index'>
        <h5><strong>{{answer._owner}}</strong>: <em> {{answer.answer}} ID {{answer._id}}</em></h5>
            <div class='replies' style='margin-left: 20px'>
            -find a way to get comments here
                <input class='Reply' ng-model='vm.newComment'>
                <input type='submit' ng-click = 'vm.Reply(answer._id)'> 
            </div>

角度控制器

            function getPosts() {
            PostsFactory.getPosts()
            .then(function(data) {
                console.log(data)
                console.log('getting POSTS')
                vm.posts = data
                console.log(map(vm.posts, getAnswers));     
            })
            .catch(function(){
                console.log('in the single psot controller and could not get posts')
            }

            function getPosts() {
            var deferred = $q.defer()
            $http.get('/getAnswers')
            .success(function(data) {
                deferred.resolve(data)
            })
            .error(function() {
                console.log('could not get posts')
            })
            return deferred.promise
        }

模型/路线/控制器

var Post = new mongoose.Schema({
    category: String,
    topic: String,
    description: String,
    points: Number,
    owner: String,
    answers: [{type: ObjectId, ref: 'Answer'}],
    date_created: Date
});

答案

var Answer = new mongoose.Schema({
  answer: String,
  _post: {type: ObjectId, ref:'Post'},
  _owner: String,
  points: Number,
  comments: [{type: ObjectId, ref: 'Comment'}],
  date_created: Date
})

评论

var Comment = new mongoose.Schema({
  comment: String,
  _answer: {type: ObjectId, ref: 'Answer'},
  _owner: String,
  points: Number,
  date_created: Date
})

控制器

posts.show = function(req, res) {
    Post.find()
    .populate('answers')
    .exec(function(err, result) {
        if(err) {
            console.log('error finding post')
        } else {
            res.json(result)
        }
    })
}

1 个答案:

答案 0 :(得分:0)

Mongoose支持&#34;多层次&#34;人口,但您需要将这些信息提供给.populate()

"populate"

所以#&#34;单一路径发生的最多&#34;是那条特别的路径&#34;填充。但是通过&#34;嵌套&#34; xs.zip(xs).flatMap(pair => List(pair._1,pair._2)) 参数,然后进一步调用其他引用的名为。

的项