MokoJs / mongo

mongo adapter for Moko
6 stars 0 forks source link

Doesn't update nested attribute #2

Closed ignacioola closed 10 years ago

ignacioola commented 10 years ago

When updating a nested property of an existent model and saving it, it doesn't sync the value to the db.

var co = require('co');
var moko = require('moko');
var mongo = require('moko-mongo');

var Person = moko('person')
  .attr('_id')
  .attr('name')
  .attr('data');

co(function*() {
  var db = yield mongo('mongodb://localhost:27017/test');

  Person.use(db);

  var p = yield new Person({
    name: 'xxx',
    data: {}
  });

  yield p.save();

  p = yield Person.get(p._id);

  // update
  p.name = 'yyy';
  p.data.points = 100;

  // `name` stored ok, but `data.points` not
  yield p.save();
})();
rschmukler commented 10 years ago

This is because the object isn't "changing", just a property on it is. This is a bug in moko that I will have to fix. In the mean time, you can do either of these:

p.data = p.data;
p.data.points = 100;

// or

p.data = { points: 100 };