解析JS SDK: relation.add()不起作用
Parse JS SDK: relation.add() not working
我需要在Parse JS SDK中添加对象到关系,但我不能使relation.add()
工作,我找不到我的代码和文档示例之间的差异或我搜索的答案。我的功能:
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();
因为标签在产品文档中是一个关系数组——是一个列。所以你必须处理product对象本身。而不是标签col.
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- JsFiddle上的鼠标事件不起作用
- 我的AngularJS表达式没有'不起作用
- 点击按钮输入不起作用
- 面向对象的Javascript代码在IE7中不起作用
- 分部隐藏在jquery中不起作用
- 在phonegap应用程序内部重定向不起作用
- Javascript add class 不起作用
- 使用引导模式形式的ADD不起作用
- 简单的jquery+javascript不起作用:add".有错误”;不工作,并且未定义onclick事件
- Jquery add/removeClass不起作用
- 在jquery中,react中的Add to link to URL不起作用
- 解析JS SDK: relation.add()不起作用
- onsuccess和oncomplete回调对indexedDb add事务不起作用
- jQuery Add Class没有'不起作用