如何显示从parse.com中的表中获取的多个记录

时间:2014-06-16 08:27:47

标签: angularjs parse-platform

我正在使用Parse作为我的应用程序。我有两个表StudentDetail和Subject,当用户输入他的名字时,从studentDetail表中查询id。并使用该ID我想获取Subject table的其余细节。通过此代码获取的数据是正确的但在$ scope.subs即19行它覆盖数据并仅返回最后一条记录,我想存储所有记录在$ scope.subs对象迭代。

$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);
      }
    });
  }

1 个答案:

答案 0 :(得分:0)

您正在将$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);

或者,如果你使用像Underscore库这样的东西,你可以只映射项目:

// 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);
相关问题