Right way to perform an update
Hi all!
I’m trying to update an entity and I don’t know if I using the correct approach.
I’m using the mysql backend and I’ve receiving this error every time I call Entity’s constructor:
Error: immutable field: id
at Object.set (/Users/leandro/w/persistencejs/lib/persistence.js:67:51)
at Observable.Entity (/Users/leandro/w/persistencejs/lib/persistence.js:493:25)
at /Users/leandro/w/proj/entity.js:60:16Am I doing something wrong?
Best regards.
Leandro.================================
this.Entity = persistence.define(‘Entity’, tableInfo);
update: function(id, entity) {
var that = this;
var session = persistenceStore.getSession();Submitted by leandro batista on 31 December 2010 at 04:48session.transaction(function(tx) { session.add(new that.Entity(session, entity)); session.flush(tx, function() { session.close(); }); }); }
Issue Log
Hi Leandro, what’s in the
entity
variable there? If it’s something that has anid
property, that may be causing the problem.
Hi Zef,
The entity variable holds a value that was generated by bodyDecoder, a middleware connect’s function. (http://senchalabs.github.com/connect/bodyDecoder.html)
Here the snippet:
exports.update = function(req, res, next) {
var id = req.params.id;
// req.body contais a json and what generated by bodyDecoder()
// it looks like a simple json object.var new_entity = req.body; Entity.Collection.update(id, new_entity);
};
Thank you.
[ ]s
leandro.
The problems seems to be that it tries to set the
.id
field from thereq.body
, which is immutable. If you make sure thatreq.body
no longer contains theid
field it should work. For instance using:delete req.body.id;
Log in to post comments