Jiew Meng
Jiew Meng

Reputation: 88197

Parse.com create object with a relation

In Parse, I can create an object with a relation like:

var obj = new Something();
var relation = obj.relation('x');
relation.add(somethingElse);
obj.save();

But how do I do the same with the REST API? I find that I cannot just pass an array of pointers.

I get something like:

invalid type for key members, expected relation<_User>, but got array.

In the docs, it says I can add objects to a relation with the update API, but no mention with the create API.

Upvotes: 0

Views: 107

Answers (1)

Robert Rowntree
Robert Rowntree

Reputation: 6289

if you want a parse property to be array of pointers to say _User, then just make sure that its property is of correct type (empty javascript array) when you create the object ...

and then use the "addUnique()" , "add()" or "remove()" methods to update the content of the array. sample below with pointers to _User in array..

var _user;
var query = new Parse.Query(Parse.Object.extend("MyClz"));
query.include("arrayPointer");
Parse.User.current().fetch().then(function (user) {
  _user = user;
  return query.get(_OID-MyClz);
}).then(function(myClz){
  myClz.addUnique("arrayPointer",_user);
  return myClz.save(); 

in Rest it looks like below:

{"myArray":{"__op":"AddRelation","objects":[{"__type":"Pointer","className":"_User","objectId":""}]}}

Upvotes: 1

Related Questions