2016-10-30 5 views
0

구문 분석 JS SDK에서 관계에 개체를 추가해야하지만 relation.add()을 사용할 수 없으며 문서 또는 답변의 코드와 예제 간의 차이점을 찾을 수 없습니다. 나는 봤다. 그것에서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(); 

의 사용과 생각 - 제품 문서 - 열입니다. 그래서 제품 객체 자체를 처리해야합니다. 꼬리표 col 아닙니다.

관련 문제