0

I would like to know in cloud code if the request is adding or removing an item from a ParseRelation. How to get information from the Parse Operation. For now I cannot get any information from Parse.Op.

for i.e : in a beforeSave() method :

var parseOp = myObject.op('myRelation');
if (parseOp != null) {
    console.log("Operations on myRelation  = " + parseOp.toJSON() );
}

in log this display only : Operations on myRelation = [object Object]

There is no information in Parse SDK js documentation. on how to use ParseOp.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ThierryC
  • 1,794
  • 3
  • 19
  • 34

1 Answers1

2

Check out the issue here regarding this:
https://github.com/ParsePlatform/Parse-SDK-JS/issues/133

If you still want to use .op you can try something like this:

var parseOp = myObject.op('myRelation');
if (parseOp) {
    console.log('array of objectIds to add', parseOp.relationsToAdd)
    console.log('array of objectIds to remove', parseOp.relationsToRemove)
    // do more things here
}

Note: you can pass multiple arguments to console.log(...) and objects will be printed out

JeremyPlease
  • 666
  • 6
  • 11
  • Thanks, I will check this. Since we should avoid to use Parse.Op directly (according to your link), do you have an idea on How to increment a counter when an object is added (or removed) from a ParseRelation ? – ThierryC Nov 30 '16 at 10:10
  • You can increment the counter wherever you're adding to the relation. Or you can run a `find` on the relation and get the length of the results. – JeremyPlease Nov 30 '16 at 14:10
  • The increment must be from server side for data integrity: for i.e.: when a user like a Post he add it to his ParseRelation "liked". But the number of Likes for this post can't be incremented by the user himself (to avoid cheating). I can't query a find and count all the time as well (a post may be liked by more than 10000 users). – ThierryC Nov 30 '16 at 16:43
  • @toofoo Based on that I would recommend using the `parseOp` code I posted in the answer. In the future, just be mindful whenever upgrading to a new version of Parse Server (ideally with some unit tests). – JeremyPlease Dec 01 '16 at 20:48