2016-10-30 1 views
0

Parse JS SDKのリレーションにオブジェクトを追加する必要がありますが、relation.add()を動作させることはできません。コードと例の違いをドキュメントや回答から見つけることはできません私はgoogled。 My機能: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

+0

どのようなエラーが表示されますか? – Soviut

+0

コンソールにエラーはありません。それは、すべての 'catch()'が適切に配置されているように見えるためです – kemsbe

答えて

2

私はあなたがタグがリレーショナル配列なので

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

代わりの

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

を使用するように持っていると思う - コラムです - 製品のドキュメントに。 したがって、商品オブジェクト自体を処理する必要があります。タグの列ではありません。

関連する問題