[英]Can't push to array using mongoose
I have code, basically still the MEANJS boilerplate, and I added a section to the articles for commenting. 我有代码,基本上仍然是MEANJS样板,并且在文章中添加了一段以供评论。 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).
我的注释策略是使用一个非常简单的注释模型(具有用户对象,内容字符串和喜欢的数字)以express / comments /:commentId公开路由。 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.
我扩展了文章模型,以包括用于评论的对象ID数组,并且在加载文章时,我的有角资源将调用/ comments /:commentId来检索由该数组指定的评论列表。 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. 除了exports.updateArticleComments函数之外,所有内容(我的意思是所有内容)均有效。 I have seriously written about 5 different function, trying lodash's _extend, and many other techniques.
我认真地写了5种不同的函数,尝试了lodash的_extend以及许多其他技术。 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 编辑:被要求共享createComment,所以在这里
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.
我也很好奇,如果注释_id的值又回来了并且不是未定义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.