6

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?

1 Answer 1

2
+100

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Adding dispatch() for AddRelation did the trick for me. Thanks Kevin!
You can also pass an array of objects as the last parameter in AddRelation. For ex. you can pass an array of User objects if you want to add that relation to a class.
@muttapp np, glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.