I have code, basically still the MEANJS boilerplate, and I added a section to the articles for commenting. My strategy for the comments was to expose a route in express, /comments/:commentId, with a very simple comment model (it has a user object, a content string, and a likes number). I extended the article model to include an array of object IDs for the comments, and when an article was loaded, my angular resources would make a call to the /comments/:commentId to retrieve the list of comments specified by the array. Following is my server code
/* below is comments.server.controller.js */
/* THIS IS NEVER GETTING CALLED */
exports.updateArticleComments = function(req, res){
Article.findById(req.comment.article).populate('user', 'displayName').exec(function(err, article){
console.log(article);
if (err) return res.json(err);
if (!article) res.json({err: 'oops!'}); //handle this ish
article.comments[article.comments.length] = req.comment._id;
article.save(function(err, article){
if (err){
console.log('error');
} else {
res.json(article);
}
});
});
};
exports.commentsByID = function(req, res, next, id) {
Comment.findById(id).populate('user', 'displayName').exec(function(err, comment) {
if (err) return next(err);
if (!comment) return next(new Error('Failed to load comment ' + id));
req.comment = comment;
next();
});
};
/* end comments.server.controller.js */
/* begin articles.server.routes.js */
'use strict';
/**
* Module dependencies.
*/
var users = require('../../app/controllers/users.server.controller'),
articles = require('../../app/controllers/articles.server.controller'),
comments = require('../../app/controllers/comments.server.controller');
module.exports = function(app) {
// Article Routes
app.route('/articles')
.get(articles.list)
.post(users.requiresLogin, articles.create);
app.route('/articles/:articleId')
.get(articles.read)
.put(users.requiresLogin, articles.hasAuthorization, articles.update)
.post(comments.createComment, comments.updateArticleComments)
.delete(users.requiresLogin, articles.hasAuthorization, articles.delete);
// Finish by binding the article middleware
app.param('articleId', articles.articleByID);
};
/* end articles.server.routes.js */
Everything, and I mean everything, works, EXCEPT exports.updateArticleComments function. I have seriously written about 5 different function, trying lodash's _extend, and many other techniques. I can't figure out why the comments array is never being filled. Does anyone have any suggestions at all?
EDIT: was requested to share createComment, so here it is
exports.createComment = function(req, res, next){
var comment = new Comment({content: req.body.content, article: req.body.articleId});
comment.user = req.user;
comment.save(function(comment, err){
if (err){
return res.jsonp(err);
} else {
req.comment = comment;
next();
}
});
};
Article.update({_id: 'VAL' }, {
$push: {
'comments' : req.comment._id }},{upsert:true},
function(err, data) { });
Have you tried the push method? I'd also be curious if the value of comment _id is coming back and not undefined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.