Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new object with relation in Parse with React Native

There seems to be no documentation on how to create a new Parse Object with a Relation with Parse-ReactNative using ParseReact.Mutation.Create. I resorted to this:

function createRow(relatedObject, data) {
  ParseReact.Mutation.Create('objectClass', data)
    .dispatch()
    .then(function(newRow) {
      ParseReact.Mutation.AddRelation(newRow, 'relationColumn', relatedObject)
    });
}

Which is creating the new objectClass Parse Object, but the column relationColumn doesn't display the relation with the given relatedObject.

Any idea on how this can be done, preferably in one query with the ParseReact.Mutation.Create?

like image 896
Naoise Golden Avatar asked Sep 30 '15 16:09

Naoise Golden


1 Answers

so my code is working with ParseReact.Mutation.AddRelation, here it is:

ParseReact.Mutation.AddRelation({
                          "className": newRow.className,
                          "objectId": newRow.id.objectId}, 'groceryRequestRelation',[{
                          "__type":"Pointer",
                          "className":"ClassThePointerPointingTo",
                          "objectId":"ObjectIdOfClassThePointerIsPointingTo"
                        }]).dispatch()

@naoisegolden you didn't put .dispatch() at the end of the AddRelation so that might be an issue.

Another thing that took me forever to figure out was I was using the wrong objectId within my AddRelation, I should be using the objectId of the class that the pointer is pointing to, but I was using the objectId of the class that the relation belongs to...

If this still doesn't work for you, try the REST way to do this:

var data = {
      "groceryRequestRelation":{"__op":"AddRelation",
      "objects":
      [{
        "__type":"Pointer",
        "className":"ClassThePointerIsPointingTo",
        "objectId":"ObjectIdOfClassThePointerIsPointingTo"
      }
      ]
    }
  };

        var url = "https://api.parse.com";
        url += "/1/classes/Classname/objectId";
        fetch(url, {
            method: 'put',
            headers: {
                'Accept': 'application/json',
                'X-Parse-Application-Id': PARSE_APP_ID,
                'X-Parse-REST-API-Key': PARSE_REST_KEY,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
        .then((response) => {
          console.log("inside add relation rest")
          console.log(response);
          return response.json();
        })
        .then((responseText) => {
          console.log(responseText);
        })
        .catch((error) => {
          console.warn(error);
        })
        .done();
like image 54
Kevin Zhao Avatar answered Sep 19 '22 23:09

Kevin Zhao