Parse-server: Updating attributes in beforeSave not working

Created on 6 Mar 2016  路  7Comments  路  Source: parse-community/parse-server

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

Environment Setup

  • Localhost
  • Node 5.7
  • MongoDB 3.2.3

    Steps to reproduce

In main.js add

  • define a test class
  • define before save for it to update 1 attribute
  • define a cloud function that you can use to try this via Postman or something
  • I expect the beforeSave to set/update an attribute but its not happening ...
var Testing1 = Parse.Object.extend('Testing1');

Parse.Cloud.beforeSave('Testing1', function(req, res) {
    var obj = req.object;
    console.log('In b4 save Testing1', obj.toJSON());

    obj.set('override', Date.now());
    res.success(obj);
});

Parse.Cloud.define('test', function(req, res) {
    var t1 = new Testing1({ name: 'X' });

    t1.save()
        .then(o => {
            console.log('After saving', o.toJSON());
            res.success(o);
        })
        .fail(err => res.error(err.message));
})

The result:

{
  "result": {
    "name": "X",
    "createdAt": "2016-03-06T10:22:20.041Z",
    "updatedAt": "2016-03-06T10:22:20.041Z",
    "objectId": "eyw0agyTof",
    "__type": "Object",
    "className": "Testing1"
  }
}

override attribute not set as expected

Logs/Trace

Most helpful comment

I am using version 2.2.7 and seems that after you changed the request.object your have to call response.success() rather than response.success(request.object)

All 7 comments

I was just working on this and submitted #865 to correct this for an older issue. :+1: It will be fixed in the next release.

@gfosco awesome, but wonder if theres a workaround for the time being? I use beforeSave to modify stuff in alot of places ...

@gfosco Any timeline on the next release with this fix?

+1, any info?
Still having this issue on "parse-server": "^2.1.5", (just re-run the code example of @jiewmeng with the same result)
Shouldn't the 2.1.5 release have fixed it? Am I missing something?
Thanks.

I still seem to have this problem on version 2.2.4. Am I doing something wrong here? My code looks identical to the example @jiewmeng provided.

I am using version 2.2.7 and seems that after you changed the request.object your have to call response.success() rather than response.success(request.object)

@ flovilmart , @gfosco , it seems that @flavionegrao solution works. It seems that if in the beforeSave I add a new field to the object being save and call response.success("some string) then the new field is not being added to the object being saved, but if I do response.success() it does add the filed?

Was this page helpful?
0 / 5 - 0 ratings