0

in my Angular app I'm trying to display a set of results that come from three Classes. Data is stored on Parse.com.

I can do just fine with the Pointer relation type for one-to-one (associated scores are being returned).

Problem starts when I try to include data from the Location class into my final returned JSON.

Hopefully this is just a basic notation thing I'm missing. Thanks a lot!

Doctors

  • String: firstname
  • Pointer: score
  • Relation: location

Scores

  • Number: scoreOne
  • Number: scoreTwo

Locations

  • String: name
  • String: address

      .controller('BrowseCtrl', function($scope) {
    
            // Get "Doctors" and associated "Scores"
            // via Pointer in Doctors Class named "score"
            var query = new Parse.Query("Doctors");
            query.include("score");
            query.find()
            .then(function(result){
    
              var doctorsArray = new Array();
    
              for(i in result){
    
                var obj = result[i];
    
                var doctorIds = obj.id;
                var docFirstname = obj.get("firstname");          
                var mainScore = obj.get("score").get("mainScore");
    
                doctorsArray.push({
                  Scores:{
                    DocMainScore: mainScore
                  },
                  firstname: docFirstname,
                });
    
    
              }
    
              // Get Locations.
              // -can be more than one per Doctor
              // Class Doctors has a Relation column "location" pointing to Locations
              var locationsArray = new Array();
              var locationRelation = obj.relation('location');
              var locationQuery = locationRelation.query();
    
              locationQuery.find({
                success: function(locations) {
                  for(j in locations){
                   var locObj = locations[j];
                   var locName = locObj.get("name"); 
                   console.log(locName);
                 }
               }
             })
    
              // send data to the view
              $scope.myData = doctorsArray;
              console.log(doctorsArray);
            });
          })
    

What I am trying to do is get the data from Locations into the doctorsArray.

1 Answer 1

1

First of all, you are using obj outside the for where it's assigned. This way it'll only get location for the last doc, I'm pretty sure it's not what you wanted.

What you want must be something like this:

// inside your Doctors query result

Parse.Promise.when(

  // get locations for all doctors
  result.map(function(doc) {
    return doc.relation('location').query().find();
  })

).then(function() {
  var docsLocations = arguments;

  // then map each doctor
  $scope.myData = result.map(function(doc, i) {
    // and return an object with all properties you want
    return {
      Scores: {
        DocMainScore: doc.get('score').get('mainScore')
      },
      firstname: doc.get('firstname'),
      locations: docsLocations[i]
    };
  });

});
Sign up to request clarification or add additional context in comments.

9 Comments

This works in adding data to the same JSON but the problem is that it is only returning an address object for the first Doctor object only. Thanks for your help!
Ok, I think you edited the code and I used a slightly previous version. Let me adjust to your edit.
Same problem: only the first doctor has a locations.Object attached. Others have: locations: undefined
Sorry, I think it's fixed now, please let me know if you managed to make it work
Thanks a lot Pacheco, no need for sorry, your assistance is greatly appreciated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.