2016-01-17 61 views
0

我有一點麻煩,讓我目前的項目工作在這一刻......我知道我沒有遵循一些最佳實踐,當涉及到結構,但這僅僅意味着對功能的測試,直到我能夠正常工作,然後才能正確地構建它。建立一個簡單的測試項目論壇與平均堆棧

我正在建設一個簡單的論壇,包括論壇,帖子和評論。我基於這個thinkster.io教程,https://thinkster.io/mean-stack-tutorial#opening-rest-routes

董事會和職位我有運作,但增加意見是另一回事。我嘗試添加一個新的Comment模式並定義一些服務器端路由以允許我獲取和發佈評論,但是這並沒有解決。現在我的策略是隻將註釋放入我的貓鼬postSchema中,然後在用戶單擊視圖註釋按鈕時隱藏它們和註釋表單或者使用角度注入HTML。這不起作用,因爲我的提交不會更新視圖或數據庫。這是我目前正在處理的內容。

app.js

(function() { 
angular.module('test', ['ui.router', 'ngMaterial']) 
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) { 
    $stateProvider 
     .state('home', { 
      url: '/home', 
      templateUrl: '/views/home/home.html', 
      controller: 'MainController', 
      resolve: { 
       boardPromise: ['boards', function (boards) { 
        return boards.getAll(); 
       }] 
      } 
     }) 

     .state('boards', { 
      url: '/boards/{id}', 
      templateUrl: '/views/posts/posts.html', 
      controller: 'PostsController', 
      resolve: { 
       board: ['$stateParams', 'boards', function ($stateParams, boards) { 
        return boards.get($stateParams.id); 
       }] 
      } 
     }); 



    $urlRouterProvider.otherwise('home'); 
}]); 
}()); 

然後我的控制器如下:

主控制器

(function() { 
angular.module('test') 
.controller('MainController', ['$scope', 'boards', function($scope, boards) { 
    $scope.boards = boards.boards; 

    $scope.addBoard = function() { 
     if ($scope.title === '') { 
      return; 
     } 
     boards.create({ 
      title: $scope.title, 
      description: $scope.description, 
     }); 

     $scope.title = ''; 
     $scope.description = ''; 
    }; 
}]); 
}()); 

帖子控制器

(function() { 
'use strict'; 

angular.module('test') 
.controller('PostsController', ['$scope', 'boards', 'board', function ($scope,     boards, board) { 
    $scope.board = board; 

    $scope.addPost = function() { 
     if ($scope.body === '') { 
      return; 
     } 

     boards.addPost(board._id, { 
      body: $scope.body, 
      author: 'user', 
     }).success(function (post) { 
      $scope.board.posts.push(post); 
     }); 
     $scope.body = ''; 
    }; 

    $scope.addComment = function() { 
     if ($scope.commentts === '') { 
      return; 
     } 

     boards.addComment(board._id, { 
      comments: $scope.comments, 
      author: 'user', 
     }).success(function (comment) { 
      $scope.board.posts.comments.push(comment); 
     }) 
     $scope.comments = '' 
    }; 
}]); 
}()); 

服務處理我的$ HTTP,

(function() { 
'use strict'; 

angular.module('test') 
.factory('boards', [ '$http', function($http) { 
    var o = { 
     boards: [] 
    }; 

    o.getAll = function() { 
     return $http.get('/boards').success(function (data) { 
      angular.copy(data, o.boards); 
     }); 
    }; 

    o.create = function(board) { 
     return $http.post('/boards', board).success(function (data) { 
      o.boards.push(data); 
     }); 
    }; 

    o.get = function(id) { 
     return $http.get('/boards/' + id).then(function (res) { 
      return res.data; 
     }); 
    }; 

    o.addPost = function (id, post) { 
     return $http.post('/boards/' + id + '/posts', post); 
    }; 

    o.addComment = function (id, comment) { 
     return $http.post('/boards/' + id + '/posts', comment); 
    }; 


    return o; 
}]); 
}()); 

這是我的後端的相關部分:

'use strict'; 

var mongoose = require('mongoose'), 
Board = mongoose.model('Board'), 
Post = mongoose.model('Post'); 

module.exports = function (app) { 

app.get('/boards', function (req, res, next) { 
    Board.find(function (err, boards) { 
     if (err) { 
      return next(err); 
     } 

     res.json(boards); 
    }); 
}); 

app.post('/boards', function (req, res, next) { 
    var board = new Board(req.body); 

    board.save(function (err, board) { 
     if (err) { 
      return next(err); 
     } 

     res.json(board); 
    }); 
}); 

app.param('board', function (req, res, next, id) { 
    var query = Board.findById(id); 

    query.exec(function (err, board) { 
     if (err) { 
      return next(err); 
     } 

     if (!board) { 
      return next(new Error('can\'t find the boards')); 
     } 

     req.board = board; 
     return next(); 
    }); 
}); 

app.param('post', function (req, res, next, id) { 
    var query = Post.findById(id); 

    query.exec = function (err, post) { 
     if (err) { 
      return next(err); 
     } 

     if (!post) { 
      return next(new Error('can\'t find the post')); 
     } 

     req.post = post; 
     return next(); 
    }; 
}); 

app.param('comment', function (req, res, next, id) { 
    var query = Comment.findById(id); 

    query.exec = function (err, comment) { 
     if (err){ 
      return next(err); 
     } 

     if (!comment) { 
      return next(new Error('can\'t find the comment')); 
     } 

     req.comment = comment; 
     return next(); 
    }; 
}); 

app.get('/boards/:board', function (req, res, next) { 
    req.board.populate('posts', 'comments', function (err, board) { 
     if (err) { 
      return next(err); 
     } 
     res.json(board); 
    }); 
}); 

app.post('/boards/:board/posts', function (req, res, next) { 
    var post = new Post(req.body); 
    post.board = req.board; 

    post.save(function (err, post) { 
     if (err) { 
      return next(err); 
     } 

     req.board.posts.push(post); 
     req.board.save(function (err, board) { 
      if (err) { 
       return next(err); 
      } 

      res.json(post); 
     }); 
    }); 
}); 

}; 

以及相應的模式:

Board模式:

'use strict'; 

var mongoose = require('mongoose'), 
Schema = mongoose.Schema; 

var boardSchema = new Schema({ 
    title: String, 
    description: String, 
    posts: [{ 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Post' 
    }] 
}); 

mongoose.model('Board', boardSchema); 

Post模式:

'use strict'; 

var mongoose = require('mongoose'), 
Schema = mongoose.Schema; 

var postSchema = new Schema({ 
    body: String, 
    author: String, 
    comments: { 
    type: String, 
    }, 
    board: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Board' 
    }, 
}); 

mongoose.model('Post', postSchema); 

任何幫助表示讚賞。一般來說,我對使用angular和MEAN堆棧相當陌生,但我真的希望變得更好。任何你們可能能夠給我的信息肯定會提高我的理解力。非常感謝! :)

回答

0

Post模式的comments字段被定義爲單個字符串。這意味着每個新評論都會覆蓋前一個。它應該被定義爲字符串的數組代替:

Post架構

var postSchema = new Schema({ 
    body: String, 
    author: String, 
    comments: [{ 
    type: String, 
    }], 
    board: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Board' 
    }, 
}); 

mongoose.model('Post', postSchema);