Skip to content

Triggering particular function inside afterSave #3268

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

Closed
srameshr opened this issue Dec 21, 2016 · 12 comments
Closed

Triggering particular function inside afterSave #3268

srameshr opened this issue Dec 21, 2016 · 12 comments

Comments

@srameshr
Copy link
Contributor

srameshr commented Dec 21, 2016

Issue Description

How do I trigger a particular function inside afterSave for a given collection.

As of now I manually set a column called updateType and then inside the afterSave block I check for the value of updateType on the request object.

Steps to reproduce

Please include a detailed list of steps that reproduce the issue. Include curl commands when applicable.

In Parse.Cloud.define()

  var query = new MyCollection();
  query.set("updateType", "update-1");
  query.save();

In Parse.Cloud.afterSave()

Parse.Cloud.afterSave('MyNewCollection', function(request, response) {
  function a() {
  }

  function b() {
  }

  if(request.updateType === 'updated-1') {
    a(); // Call function a
  }
});

Is there any other way to do this. Any option that lets me choose what block of code should be executed inside afterSave after a collection had been saved.

Expected Results

NONE

Actual Outcome

NONE

Environment Setup

  • Server

    • parse-server version (Be specific! Don't say 'latest'.) : 2.3.0
    • Operating System: MAC OSX 10.11.16 EL Capitan
    • Hardware:
    • Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): Localhost
  • Database

    • MongoDB version: db version v3.0.12
    • Storage engine: Default storage
    • Hardware: [FILL THIS OUT]
    • Localhost or remote server? (AWS, mLab, ObjectRocket, Digital Ocean, etc): Localhost

Logs/Trace

You can turn on additional logging by configuring VERBOSE=1 in your environment.

[FILL THIS OUT]

@natanrolnik
Copy link
Contributor

@srameshr have you tried moving the function a() declaration outside of Parse.Cloud.afterSave?

@srameshr
Copy link
Contributor Author

@natanrolnik Sorry, I dont understand how would that help. My point is, is there any way to pass some data from Collection.save() so that it appears in afterSave's request object.

As I have pointed out earlier, I am creating a column called updateType in my collection and setting it to some value each time on Collection.save().

@natanrolnik
Copy link
Contributor

@srameshr Yes, there is, and this is the exact reason why the beforeSave and afterSave hooks exist.

You can do:

var myObject = request.object; to access the object;

And then var updateType = myObject.get("updateType");

You can read the docs here for more examples.

@natanrolnik
Copy link
Contributor

I also recommend reading the JavaScript SDK guide.

@srameshr
Copy link
Contributor Author

@natanrolnik Tried that. It does not work the way I want. I am trying to set a property on request object in beforeSave and also trying to access the same property on the request object in afterSave. It gives out undefined

@natanrolnik
Copy link
Contributor

Can you post some of your code?

@srameshr
Copy link
Contributor Author

@natanrolnik Never mind. Setting a column called updateType makes more sense to my use case.

@natanrolnik
Copy link
Contributor

So it's working now?

@srameshr
Copy link
Contributor Author

Not with the beforeSave method.

@natanrolnik
Copy link
Contributor

So could you give some extra information? What are you trying, what are the logs you see (when running with VERBOSE=1)?

@srameshr
Copy link
Contributor Author

srameshr commented Dec 21, 2016

I will. Give me sometime. Have to run some errands.

@srameshr
Copy link
Contributor Author

srameshr commented Apr 9, 2017

@natanrolnik Here is what I am trying to achieve:
Consider User class. Now I have 3 parse define functions that modify and save the User collection.

Parse.Cloud.define('one', (request, response) => {
  // do some manipulation and save user object
})

Parse.Cloud.define('two', (request, response) => {
  // do some manipulation and save user object
})

Parse.Cloud.define('three', (request, response) => {
  // do some manipulation and save user object
})

Now the afterSave block

Parse.Cloud.afterSave(Parse.User, () => {

  // After save has 3 different functions which has to be run, based on which Parse.Cloud.define() function was called.

  function one() {}
  function two() {}
  function three(){}
  
  if( Parse.Cloud.define('one') was called ) {
    one() 
  }
  if( Parse.Cloud.define('two') was called ) {
    two() //
  }
  // So on
    
});

Now, afterSave will be triggered after every Parse.Cloud.define block is run. How do I identify, which Parse.Cloud.define block triggered the afterSave inside the afterSave so that I can invoke the right function.

@andrewimm

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

2 participants