【问题标题】:Can't push to array using mongoose无法使用猫鼬推送到阵列
【发布时间】:2015-10-22 07:16:14
【问题描述】:

我有代码,基本上仍然是 MEANJS 样板,我在文章中添加了一个部分以供评论。我对 cme​​ts 的策略是用一个非常简单的评论模型(它有一个用户对象、一个内容字符串和一个点赞数)在 express 中公开一个路由 /cmets/:commentId。我扩展了文章模型以包含 cmets 的对象 ID 数组,当加载文章时,我的角度资源将调用 /cmets/:commentId 以检索数组指定的 cmets 列表。以下是我的服务器代码

/* 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 */

一切,我的意思是一切,工作,除了exports.updateArticleComments 函数。我认真地写了 5 种不同的函数,尝试了 lodash 的 _extend 和许多其他技术。我无法弄清楚为什么 cmets 数组永远不会被填充。有没有人有任何建议?

编辑:被要求分享 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();
    }
  });
};

【问题讨论】:

  • 你也可以上传comments.createComment函数吗?从未调用过平均第一个函数comments.createComment从未调用过next
  • @greenlikeorange 确定,它起来了。感谢您的宝贵时间!

标签: arrays angularjs node.js mongodb mongoose


【解决方案1】:
Article.update({_id: 'VAL' }, {
  $push: { 
    'comments' : req.comment._id }},{upsert:true},
    function(err, data) { });

你试过推送方法吗?我也很好奇评论 _id 的值是否会返回而不是未定义。

【讨论】:

  • 我试过这段代码,但没有用。我已经尝试过推送方法。就像我说的那样,我认为我的代码挂在了其他地方,因为从 updateArticleComments 中我根本看不到 anything。在出现其他任何内容之前,甚至没有 console.log('random');
猜你喜欢
  • 2018-06-02
  • 2019-10-13
  • 1970-01-01
  • 2018-12-30
  • 2017-06-02
  • 2017-01-11
  • 2018-11-22
  • 2016-01-29
  • 2019-03-22
相关资源
最近更新 更多