2014-06-16 2 views
0

내 응용 프로그램에 대해 구문 분석을 사용하고 있습니다. 두 테이블 StudentDetail 및 Subject 사용자가 자신의 이름을 입력 할 때 ID는 studentDetail 테이블에서 쿼리됩니다. 이 id를 사용하여 제목 테이블의 세부 정보를 나머지 부분을 가져 오려고합니다.이 코드를 통해 가져온 데이터는 정확하지만 $ scope.subs 즉 19 번째 줄에서 데이터를 무시하고 마지막 레코드 만 반환합니다. 모든 레코드를 저장하려고합니다. in $ scope.subs 객체를 반복적으로 사용합니다. 당신은 $scope.subs을 설정하는parse.com의 테이블에서 가져온 여러 레코드를 표시하는 방법

$scope.subjectFetch= function(form,form1) { 
    var query = new Parse.Query(StudentDetail); 
    var querySub = new Parse.Query(Subject); 
    query.equalTo("Firstname",form1.Firstname); 
     query.find({ 
     success: function(results) { 
      stdId = results[0].id; 
      querySub.equalTo("StudentId",stdId); 
      querySub.find({ 
        success: function(subjects) { 
         alert("Success"); 
        for (var i = 0; i < subjects.length; i++) { 
        var object = subjects[i]; 
         subname = object.get('Name'); 
         credits = object.get('credits'); 
         code =object.get('code'); 
         duration = object.get('duration'); 
         alert("Subject name: "+subname+"\n Credits :"+credits+"\n Code :"+code+"\n Duration:"+duration); 
        $scope.subs=[{Name:subname,credits:credits,code:code,duration:duration},{Name:"xyz",credits:5}]; 
        //console.log($scope.subs); 
       } 
       }, 
       error: function(error) { 
       alert("Error: " + error.code + " " + error.message); 
       } 
      }); 
     }, 
      error: function(error) { 
     alert("Error: " + error.code + " " + error.message); 
     } 
    }); 
    } 

답변

0

는이 완료되면 마지막 일이 왜 루프, 내부 거기에 하나의 객체로 새로운 배열이 있습니다.

대신 다음과 같은 성공 핸들러를 시도해보십시오

// replace success function body with this: 
$scope.subs = _.map(subjects, function(subject) { 
    return { 
     Name: subject.get('Name'), 
     credits: subject.get('credits'), 
     code: subject.get('code'), 
     duration: subject.get('duration') 
    }; 
}); 
console.log($scope.subs); 
:

success: function(subjects) { 
    // reset subs array 
    $scope.subs = []; 
    for (var i in subjects) { 
     var subject = subjects[i]; 
     // push new object into the array 
     $scope.subs.push({ 
      Name: subject.get('Name'), 
      credits: subject.get('credits'), 
      code: subject.get('code'), 
      duration: subject.get('duration') 
     }); 
    } 
    // log the populated array 
    console.log($scope.subs); 

을 당신이 밑줄 라이브러리 같은 것을 사용 또는 경우에, 당신은 항목을 매핑 할 수 있습니다

관련 문제