1

I have a two calls,for match and matches. Match gets the data for one specific match, and matches the whole batch, with some relative ratings. I need to pass those relative ratings to a matching specific match.

Service that returns the json data from different API endpoints

function matchService($http, $q, API_URL) {

 function getMatchesForVacancy(vacancyId) {
  var deferred = $q.defer();

  $http.get(API_URL + vacancyId + '/matches') //v2.0 lib
    .then(function matchesArrived(matchData) {
      deferred.resolve(matchData.data);
    }, function matchesFailed(error) {
      deferred.reject(error);
    });
  return deferred.promise;
 }

 function getMatchForVacancy(vacancyId, accountId) {
  var deferred = $q.defer();
  $http.get(API_URL + vacancyId + '/matches/' + accountId)
    .then(function matchesArrived(matchData) {
      //matchData.data.candidateRating = {overallScore: 0.65}; - ie. what I need to get
      deferred.resolve(matchData.data);
    }, function matchesFailed(error) {
      deferred.reject(error);
    });
  return deferred.promise;
 }

Matches and match are displayed in the different views, so I have separate controller files. Now in the single match controller I am trying to pass in the specific rating where id's are the same.

So what I tried to do, was in candidate controller create a function like this:

function connectRating(vacancyId, match){
  var matches = matchService.getMatchesForVacancy(vacancyId);
  for(var i = 0; i < matches.length; i++){

    if(matches[i].accountId === match.accountId){
      match.candidateRating.overralScore = matches[i].candidateRating.overralScore;
      return match;
    }  
  }
}

The json for matches is like this:

  [
   {
    "accountId": 0,
    "candidateRating": {
      "overallScore": 0
     }
   }
  ]

And somehow the right data is not passed, or anything for that matter. I must be missing something, so I would appreciate any help.

1 Answer 1

2

You are missing the successful function of your promise. That's when you have a guarantee of the asynchronous result. Try this:

matchService.getMatchesForVacancy(vacancyId).then(function(d){
   // the return data is in "d"
   // do whatever you want with it
});
Sign up to request clarification or add additional context in comments.

Comments

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.