【问题标题】:Angular/Node/Mongoose - Rendering Post, Comment, RepliesAngular/Node/Mongoose - 渲染帖子、评论、回复
【发布时间】:2016-08-09 16:30:59
【问题描述】:

我正在创建一个人们可以提问的应用程序。回答 - 并回复答案。我可以毫无问题地呈现帖子和答案,但无法获得对答案的回复。有人可以给我有关最佳方法的提示吗?


html页面

    <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)
        }
    })
}

【问题讨论】:

    标签: javascript angularjs node.js mongodb mongoose


    【解决方案1】:

    Mongoose 支持“多级”人口,但您需要将该信息提供给.populate()

    posts.show = function(req,res) {
      Post.find()
        .populate({
          "path": "answers",
          "populate": {
            "path": "comments",
            "model": "Comment"  
          }
        })
        .exec(function(err,result) {
            if (err) {
               console.log(err);
            } else {
               res.json(result)
            }
        });
    }
    

    因此,“单一路径”发生的最多情况就是填充了特定的“路径”。但是通过“嵌套”"populate" 参数,会进一步调用其他被命名的引用项。

    【讨论】:

      猜你喜欢
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多