解析 JS SDK:relation.add() 不工作

Parse JS SDK: relation.add() not working

我需要将对象添加到 Parse JS SDK 中的关系,但我无法使 relation.add() 工作,我找不到我的代码与文档中的示例或我用 google 搜索的答案之间的差异。我的函数:

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);
            });
        });
    }

console.log()的输出:

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

我想我遗漏了一些小细节,但我真的不知道这些是什么,所以非常感谢您的帮助。 来自 package.json 的解析版本:~1.9.2

我认为你必须使用

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

而不是

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

因为标签是一个关系数组 - 是一个列 - 在产品文档中。 所以你必须处理产品对象本身。不是标签 col.