Search code examples
javascriptarrayspointersparse-platformparse-cloud-code

Parse.com issues while querying array of pointers, .include not getting nested pointer data in cloud code


I am having trouble getting data from the nested pointers in my array of pointers from a query. I have an array of pointers like so: [{"__type":"Pointer","className":"QuizData","objectId":"rmwJrV55c7"},{"__type":"Pointer","className":"QuizData","objectId":"2132q8i9np”}, etc…]

That QuizData class also has a column named “ad” which is a Pointer to the “Ads” class. I can get the QuizData in a query using the following include statements on my query like so:

var __quizAdQueueQuery = new Parse.Query(QuizAdQueue);
__quizAdQueueQuery.equalTo("user", __request.user);
__quizAdQueueQuery.include("quizAdArr”);
__quizAdQueueQuery.include(["quizAdArr.QuizData"]);

BUT Neither of these or both combined don’t work as when I try to get column data from the ad it’s always undefined:

__quizAdQueueQuery.include(["quizAdArr.QuizData.ad"]);
__quizAdQueueQuery.include(["quizAdArr.QuizData.Ads"]);

This is my return from that query, where the column data "mediaType" that I am trying to access is always undefined:

return __quizAdQueueQuery.first().then(function(__resultsObj)
{

  __quizQueueObj = __resultsObj;
  __userQuizQueueArr = __quizQueueObj.get("quizAdArr");

  var __quiz;
  var __ad;
  var __seenAd;
  var __lengthInt = __userQuizQueueArr.length;
  var __mediaTypeStr = __request.params.mediaType;
  var __matchedQuizzesArr = [];

  for (var __i = 1; __i < __lengthInt; __i++)
  {
    __quiz = __userQuizQueueArr[__i];
    // console.log('__quiz.get("name") = '+__quiz.get("name"));
    __ad = __quiz.get("ad");
    // console.log("__ad.id = "+__ad.id);
    //THE MEDIA TYPE IS ALWAYS RETURNING UNDEFINED HERE!!!
    console.log('__ad.get("mediaType") = '+__ad.get("mediaType")+', __mediaTypeStr = '+__mediaTypeStr);

    if (__ad.get("mediaType") == __mediaTypeStr)
    {
      //put all matches in array to be sorted
      __matchedQuizzesArr.push(__userQuizQueueArr[__i]);
      console.log("__matchedQuizzesArr.length = "+__matchedQuizzesArr.length);
    }
  }

  return __matchedQuizzesArr;
});

Thanks for any help you can give! I also posted this as a bug in the Parse/Facebook issue reporter but was redirected here, so if this is a bug I can reopen it: https://developers.facebook.com/bugs/923988310993165/

EDIT Here is the updated, working query with nested includes for clarity:

var __quizAdQueueQuery = new Parse.Query(QuizAdQueue);
__quizAdQueueQuery.equalTo("user", __request.user);
__quizAdQueueQuery.include('quizAdArr');
__quizAdQueueQuery.include('quizAdArr.ad');

Solution

  • This should work (you only need to list the column names):

    query.include('quizAdArr.ad');
    

    Here's why:

    • You're querying QuizAdQueue so you don't need to list that
    • The QuizAdQueue class has an array in quizAdArr so you include it: query.include('quizAdArr');
    • Each quizAdArr element is a QuizData with an ad so you include it: query.include('quizAdArr.ad');

    The issue was that you were including QuizData which is the name of a class and not a column name