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

.save() doesn't trigger beforeSave and afterSave #1031

Closed
mmazzarolo opened this issue Mar 15, 2016 · 9 comments · Fixed by #1066
Closed

.save() doesn't trigger beforeSave and afterSave #1031

mmazzarolo opened this issue Mar 15, 2016 · 9 comments · Fixed by #1066

Comments

@mmazzarolo
Copy link

Hello everyone,
first of all thank you for releasing parse-server!

Now on the issue:
Attributes changed on beforeSave are still not updated, even on Parse >= 2.1.5.
When you call save() on an object on parse-server then beforeSave and afterSave are not triggered.

Environment Setup

"parse": "^1.7.1",
"parse-server": "^2.1.6",

Steps to reproduce

Just create a test class called TestClass, put the following code in you parse-server main module and call the test endpoint:

const TestClass = Parse.Object.extend('TestClass')

Parse.Cloud.define('test', (req, res) => {
  console.log('test function called')
  const test = new TestClass({ name: 'testtesttest' })
  test.save()
    .then(res.success)
    .fail(err => res.error(err.message))
})

Parse.Cloud.beforeSave(TestClass, (req, res) => {
  const obj = req.object
  console.log('[beforeSave] object: ', obj.toJSON())
  obj.set('surname', 'this is not saved')
  res.success()
})

Parse.Cloud.afterSave(TestClass, (req, res) => {
  const obj = req.object
  console.log('[afterSave] object: ', obj.toJSON())
  res.success()
})

Logs/Trace

beforeSave and afterSave are not called.

Parse-server console output:

test function called

Client that called the test function result:

{
  "result": {
    "name": "testtesttest",
    "createdAt": "2016-03-15T07:56:25.711Z",
    "updatedAt": "2016-03-15T07:56:25.711Z",
    "objectId": "SSXFO5VfxU",
    "__type": "Object",
    "className": "TestClass"
  }
}

If you create a new TestClass object by calling the classes/TestClass endpoint (e.g.: http://localhost:1337/parse/classes/TestClass) beforeSave and afterSave are triggered correctly:

Parse-server console output:

[beforeSave] object:  { name: 'testtest' }
[afterSave] object:  { createdAt: '2016-03-16T14:24:20.923Z',
  name: 'testtest',
  surname: 'this is not saved',
  updatedAt: '2016-03-16T14:24:20.923Z',
  objectId: 'c8runB61Nf' }

API call result:

{
  "objectId": "c8runB61Nf",
  "createdAt": "2016-03-16T14:24:20.923Z",
  "name": "testtest",
  "surname": "this is not saved",
  "updatedAt": "2016-03-16T14:24:20.923Z"
}
@flovilmart
Copy link
Contributor

Did you check in the database if the property was set?

@mmazzarolo
Copy link
Author

Yes, i checked, the property is not set in the db

@mmazzarolo
Copy link
Author

I have some more details:

  1. In the above example the the attribute are not updated because beforeSave is not triggered.
  2. If, instead of calling the Test function, you add a new TestClass object with a simple API call to classes/TestClass (e.g.: http://localhost:1337/parse/classes/TestClass) then beforeSave is triggered and attributes are saved/updated correctly.

Hope it helps.

@mmazzarolo mmazzarolo changed the title Attributes changed in beforeSave are not updated .save() doesn't trigger beforeSave and afterSave Mar 16, 2016
flovilmart added a commit that referenced this issue Mar 17, 2016
flovilmart added a commit that referenced this issue Mar 17, 2016
@flovilmart
Copy link
Contributor

Check on the PR I just posted, I was working in this area, added a test with creating an object in a cloud function that gets updates by a beforeSave trigger and the returned object is properly set now.

@mmazzarolo
Copy link
Author

Hi @flovilmart, I'm a little late but it's still not working on:

    "parse": "^1.8.1",
    "parse-server": "^2.2.0",

Same result as my first post :(

@gfosco
Copy link
Contributor

gfosco commented Mar 22, 2016

I'm not able to reproduce this. Something else must be wrong. Is there anything else in your cloud code file?

> [email protected] start /Users/fjm/git/parse-server-example
> node index.js

DATABASE_URI not specified, falling back to localhost.
parse-server-example running on port 1337.
Parse LiveQuery Server starts running
test function called
[beforeSave] object:  { name: 'testtesttest' }
[afterSave] object:  { createdAt: '2016-03-22T20:52:19.976Z',
  name: 'testtesttest',
  surname: 'this is not saved',
  updatedAt: '2016-03-22T20:52:19.976Z',
  objectId: 'C10ysqMDpf' }

@brandly
Copy link

brandly commented Mar 22, 2016

I just fixed an issue in a beforeSave call on my end. The code looked something like

// request.object.get('foo') === 'bar'
request.object.set('foo', 'baz')
response.success(request.object)

but I wasn't seeing 'baz' show up in the database.

Changing the code to

request.object.set('foo', 'baz')
response.success()

where nothing gets passed into the response.success() call seemed to fix the issue. I haven't dug around parse-server to find out why, but maybe this helps y'all figure it out 💯

@gfosco
Copy link
Contributor

gfosco commented Mar 22, 2016

@brandly that's the correct method, just calling success().

@mmazzarolo
Copy link
Author

@gfosco I'm dumb.
It was my fault for not deleting node_modules after updating Parse and other packages, as always.
Did a rm -rf node_modules/ && npm cache clean && npm prune && npm i and it seems to work correctly now.

Thank you all :)

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

Successfully merging a pull request may close this issue.

4 participants