Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating attributes in beforeSave not working #866

Closed
3 tasks done
jiewmeng opened this issue Mar 6, 2016 · 7 comments
Closed
3 tasks done

Updating attributes in beforeSave not working #866

jiewmeng opened this issue Mar 6, 2016 · 7 comments

Comments

@jiewmeng
Copy link

jiewmeng commented Mar 6, 2016

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

@gfosco
Copy link
Contributor

gfosco commented Mar 6, 2016

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

@gfosco gfosco closed this as completed Mar 6, 2016
@jiewmeng
Copy link
Author

jiewmeng commented Mar 6, 2016

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

@btate
Copy link

btate commented Mar 7, 2016

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

@mmazzarolo
Copy link

+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.

@brian473
Copy link

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.

@flavionegrao
Copy link
Contributor

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)

@mmarshak
Copy link

@ 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants