0

I need to add objects to relation in Parse JS SDK, but I can't make relation.add() work, I can't find differences between my code and examples from docs or answers I googled. My function:

addTagsToProduct(productId, tags) {
        console.log(productId);
        console.log(tags);
        let query = new this.cloud.parse.Query('Products');
        query.equalTo('objectId', productId);
        query.first().then((product) => {
            console.log(product);
            product.relation('tags').query().find().then((productTags) => {
                console.log(productTags);
                let productTagIds = productTags.map((tag) => {
                    return tag.id;
                });

                tags.forEach((tag) => {
                    console.log('checking', tag);
                    if (productTagIds.indexOf(tag) === -1) {
                        console.log('adding', tag);
                        let query = new this.cloud.parse.Query('Tags');
                        query.equalTo('objectId', productId);
                        query.first().then((tag) => {
                            console.log('found', tag);
                            console.log('relation', product.relation('tags'));

                            product.relation('tags').add(tag);
                            product.relation('tags').save({useMasterKey: true}).then((product) => {
                                console.log('saved', product);
                                product.relation('tags').query().find().then((productTags) => {
                                    console.log(productTags);
                                });
                            });
                        }).catch((err) => {
                            console.error(err);
                        });
                    }
                });
            }).catch((err) => {
                console.error(err);
            });
        });
    }

And console.log()'s output from it:

pJ1B9mE34k // product ID
["pJ1B9mE34k", "pJ1B9mg34k"] // tags to add
ParseObject {_objCount: 8, className: "Products", id: "pJ1B9mE34k"} // found product
[ParseObject] // tags already added to relation (manually)
checking pJ1B9mE34k // already added tag
checking pJ1B9mg34k // new tag
adding pJ1B9mg34k // found out it's new, adding
found ParseObject {_objCount: 10, className: "Tags", id: "pJ1B9mE34k"} // found tag by id
relation ParseRelation {parent: ParseObject, key: "tags", targetClassName: "Tags"} // relation to add new object to
saved ParseObject {_objCount: 8, className: "Products", id: "pJ1B9mE34k"} // .then() of save() function
[ParseObject] // one relation, the same as before, new one wasn't saved

I think I'm missing some minor details, but I really can't find out what are these, so any help'll be very appreciated. Parse version from package.json: ~1.9.2

2
  • What error are you getting? Commented Oct 30, 2016 at 22:49
  • No errors in console, that's strange, because it looks like all catch()'es are in place Commented Oct 30, 2016 at 22:50

1 Answer 1

2

I think you have to use

let productTagsRelations = product.relation("tags");
productTagsRelations.add(tag);
product.save();

Instead of

product.relation('tags').save();

Because the tags is a relational array - is a column - in the product document. So you have to deal with the products object Itself. not the tags col.

Sign up to request clarification or add additional context in comments.

Comments

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.