2014-04-19 4 views
1

나는 query을 수행하고 이후에 results을 필터링하는 구문 분석 코드 기능을 작업 중입니다. 이 코드는 JavaScript로 작성된 첫 번째 코드 줄이므로 다음 문제를 해결하는 방법을 찾을 수 없습니다.구문 분석 코드에서 개체를 가져 오는 방법

문제는 내 필터 조건자가 someArray 변수에 저장된 일부 요소를 필요로한다는 것입니다. someArray의 모든 요소는 아직 가져 오지 않습니다. 그러나 Parse.Object를 가져 오는 것은 비동기 호출이므로 true 또는 falsefilter()에 의해 반환 될 가능성이 없습니다.

이 문제를 어떻게 해결할 수 있습니까?

아니면 내가 알고 있지만

arrayElement with name:objectundefiend 

arrayElment로 표현하는 객체 인 name 칼 럼이있는 arrayElement

console.log("arrayElement with name: ".concat(typeof(arrayElement), arrayElement.get("name"))); 

인쇄를 가져 오지 않을 때, 사실을 잘못 해석하고 항상 정의 되나요?

//The cloud code 
Parse.Cloud.define("search", function(request, response) { 
    var query = new Parse.Query("Location"); 
    query.withinKilometers("gps", request.params.searchLocation, request.params.searchRadius/1000); 
    query.find({ 
     success: function(results) { 
      // results is an array of Parse.Object. 
      var locations = results.filter(function(location) { 
       console.log("Location with name: ".concat(location.get("name"))); 
       var someArray = location.get("someArray"); 
       if (someArray instanceof Array) { 
        console.log("The array of this location has ".concat(someArray.length, " elements.")); 
        someArray.forEach(function(arrayElement) { 
         arrayElement.fetch().then(
          function(fetchedArrayElement) { 
           // the object was fetched successfully. 
           console.log("arrayElement with name: ".concat(typeof(fetchedArrayElement), fetchedArrayElement.get("name"))); 
           if (menuItem) {}; 
           return true; 
          }, 
          function(error) { 
           // the fetch failed. 
           console.log("fetch failed"); 
          }); 
        }); 
       } 
      }); 
      response.success(locations); 
     }, 
     error: function(error) { 
      // error is an instance of Parse.Error. 
      response.error(error); 
     } 
    }); 
}); 

몇 가지 유용한 링크 :이 권리를 읽고 있어요 경우

답변

4

, 당신은 구문 분석 개체의 배열 열을 가지고있다. 초기 쿼리에서 개체를 가져 오려면 쿼리에 include 메서드를 사용해야합니다.

query.include('someArray'); 
난 당신이 필터를 사용하는 것이 좋습니다 생각하지 않으며, 여기에 약속 문서를 살펴해야

: 그것은 비동기 이후 네 말이 맞아 https://parse.com/docs/js_guide#promises

는, 당신은 기다릴 필요 코드에서 현재 발생하지 않는 response.success을 호출하기 전에 모든 작업을 수행해야합니다. Promises를 사용하는 자신 만의 비동기 함수를 약속하고 정의하는 것은 기능을 연결하고 계속하기 전에 함수 그룹이 완료되기를 기다리는 좋은 방법입니다.

해당 열을 포함 무엇을하고 싶었던 모두는, 모든 것은 단지 할 필요가있는 경우 :

Parse.Cloud.define("search", function(request, response) { 
    var query = new Parse.Query("Location"); 
    query.include('someArray'); 
    query.withinKilometers("gps", request.params.searchLocation, request.params.searchRadius/1000); 
    query.find().then(function(results) { 
    response.success(locations); 
    }, function(error) { 
    response.error(error); 
    }); 
}); 
+0

유용 들리지만, 문제는 (내가 질문에 그것을 간체), 즉의 요소' someArray'는 다른 Parse Objects의 배열 열을 가진 Parse Objects입니다. 어떻게 그들을 포함시킬 수 있습니까? '응답 '을 가능한 한 작게 만들기 위해 어떻게 가져온 것들을 모두 "제거"할 수 있습니까? '필터 '는 뭐가 잘못 됐어? –

+0

도트 표기법을 사용합니다. 즉, query.include ('someArray.someOtherColumn'); .filter에 관해서는 내부에서 비동기 작업을 수행하지 않는 한 괜찮습니다. – Fosco

+0

완벽하게 작동합니다! 고마워요! –

관련 문제