BartBiczBoży
BartBiczBoży

Reputation: 2672

How to create new Parse.Object with relation in Parse-SDK-JS

Docs say it is required to create relation type column in the dashboard prior to adding any relations to object:

In the Data Browser, you can create a column on the Book object of type relation and name it authors.

Is it possible to create it from js sdk level? I tried something like here:

book.save(null).then(saved => {
  let relation = new Parse.Relation(saved.id, "authors");
  relation.add(author);
  saved.save(null);
}

But it can not be saved saying TypeError: this.parent.set is not a function

I know I can add it manually in the dashboard as stated, but the idea is to create new classes automatically.

Upvotes: 1

Views: 2828

Answers (2)

mohaned ahmed
mohaned ahmed

Reputation: 49

I will Show you how to add a pointer and relation in Parse

Parse.User.currentAsync().then((current) => {
  if (current) {
    var pharmacy = new Parse.Object("Pharmacy");
   // add a pointer here
    pharmacy.set("owner", current);
    pharmacy
      .save()
      .then((pharmacy) => {
        //   add a relation here
        var relation = current.relation("pharmacy");
        relation.add(pharmacy)
        current.save()};

  }
});

Upvotes: 0

Julien Kode
Julien Kode

Reputation: 5479

If you want to add a relation with the Javascript Parse SDK you need to work directly with your object:

For example:

var user = Parse.User.current();
var relation = user.relation("mylikes");
relation.add(post); // Post is a Parse Object
user.save();

In your case, I think you want something like that:

book.save(null).then(saved => {
    var relation = saved.relation("authors");
    relation.add(author);
    saved.save(null);
}

I hope my answer was helpful 😊

Upvotes: 2

Related Questions