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
catch()'es are in place