Search code examples
javascriptnode.jsparse-platformparse-server

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.


Solution

  • 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 😊