【问题标题】:Object.assign() Issues on Mongoose SubDocument Schema关于 Mongoose 子文档架构的 Object.assign() 问题
【发布时间】:2017-07-05 13:46:29
【问题描述】:

所以当我在特定的 Mongoose 子文档架构上使用 Object.assign(例如 Object.assign({foo:bar1},{foo:bar2}) 时,foo:bar2 obj 不会覆盖 foo:bar1对象。

在我的应用程序中,我有一个包含两个子文档架构、项目和属性的帐户架构。我在项目子文档架构中遇到了问题。

这是我的模型:

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

var ProjectSchema = new Schema({
    _id: String,
    name: String,
}, { strict: false })

var PropertySchema = new Schema({
    _id: String,
    slug: String,
    name: String,
    type: String,
    placeholder: String,
    order: Number
}, { strict: false })

var AccountSchema = new Schema({
    _id: String,
    name: String,
    slug: String,
    email: String,
    properties: [PropertySchema],
    projects: [ProjectSchema],
}, { strict: false })

module.exports = mongoose.model('Account', AccountSchema);

这是我的中间件的一个sn-p(我认为这就是它的名字,如果我错了请纠正我)。这是我执行 Object.assign 以覆盖特定项目的属性的地方。如果有帮助,我可以发布所有路线。

router.route('/accounts/:account_id/projects/:project_id')

  .get(function(req, res) {
    Project.findById(req.params.project_id, function(err, project){
      if (err) 
        res.send(err);
      res.json(project);
    });
  })

  .delete(function(req, res){
    Account.findById(req.params.account_id, function(err,account){
      if (err)
        res.send(err);

      account.projects.id(req.params.project_id).remove();

      account.save(function(err) {
        if(err)
          res.send(err);
        res.json({message: 'Project Deleted'});
      });
    })
  })

  .put(function(req, res){
    Account.findById(req.params.account_id, function(err,account){
      if (err)
        res.send(err);

      Object.assign(account.projects.id(req.params.project_id), req.body);

      account.save(function(err) {
        if(err)
          res.send(err);
        res.json({message: 'Project Updated'});
      });
    })
  });

// ----------------------------------------------------
router.route('/accounts/:account_id/properties/:property_id')

  .get(function(req, res) {
    Property.findById(req.params.property_id, function(err, property){
      if(err)
        res.send(err);
      res.json(property);
    });
  })

  .delete(function(req, res){
    Account.findById(req.params.account_id, function(err, account){
      if (err)
        res.send(err);
      account.properties.id(req.params.property_id).remove();
      account.save(function(err){
        if(err)
          res.send(err);
        res.json({ message: 'Successfully Deleted' });
      })
    })
  })

  .put(function(req, res){
    Account.findById(req.params.account_id, function(err, account) {
      if (err)
        res.send(err);

      Object.assign(account.properties.id(req.params.property_id), req.body);

      account.save(function(err) {
        if(err)
          res.send(err);
        res.json({message: 'Property Updated'});
      })
    })
  });

所以是 Put 方法给我带来了问题,我可以毫无问题地获取、发布、删除,但是 Projects 的 Put 部分是我遇到此 Object.assign 问题的地方。有趣的是,Property Schema Put 方法的一切都完美无缺,而且它与 Project 的方法基本完全相同。

如果我 console.log 项目的 Put 方法中的一些值,我会得到以下信息:

console.log(account.projects.id(req.params.project_id));
//original value logs { _id: '1486609836741', foo: 'bar' }
console.log(req.body);
//updated value logs { foo: 'barred', _id: '1486609836741' }

console.log(Object.assign(account.projects.id(req.params.project_id), req.body));
//logs the original value { _id: '1486609836741', foo: 'bar' }

所以当我记录更新值和原始值时,它们具有相同的键,Object.assign 不应该工作吗?原始的 Obj 是否比 console.log 显示的更复杂,也许原始的和更新的没有相同的键?

我有点难过,任何帮助将不胜感激。如果我需要发布更多代码,请告诉我。谢谢。

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:
    Object.assign(account.projects.id(req.params.project_id), req.body);
    

    在这里,您尝试分配一个对象,该对象是MongooseDocumentArray.id 数组上的MongooseDocumentArray.id 方法调用的结果。这只会影响函数返回的文档副本,不会影响子文档的原始数组。

    我是 Mongoose 的新手,但我建议您尝试使用 account.projects,就像使用 MongooseArray

    var project = account.projects.id( req.params.project_id ),
        index = account.projects.indexOf( project );
    
    Object.assign( account.projects[ index ], req.body);
    

    【讨论】:

    • 我得到了更新对象没有分配给原始 account.project.{targetproject} 的相同问题
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 2016-02-29
    • 2014-11-23
    • 2012-09-11
    • 2017-05-29
    • 1970-01-01
    相关资源
    最近更新 更多