Search code examples
javascriptiosparse-platformparse-cloud-code

Parse afterFind, added object to response is a pointer in client


I am working with an afterFind cloud function with Parse, that simply does an extra query and sets an extra object in the response. I am expecting this same object to be fully retrieved in the client, but it is actually a pointer or something, because I need to call fetchIfNeeded on it before accessing its data.

Parse.Cloud.afterFind("PublicationImage", function(request, response) {

    ...

    votesQuery.equalTo("author", user)
    votesQuery.containedIn("target", publicationImagesIds);
    votesQuery.find()
      .then(function(votes) {
        votes.forEach(function(vote) {
          voteTarget = vote.get("target");
          for (var i = 0; i < publicationImages.length; i++) {
            if (publicationImages[i].id == voteTarget.id) {
              publicationImages[i].set("userVote", vote); <-- Here I add an obj
            }
          }
        });
        response.success(publicationImages);
      })

    ...

});

Solution

  • I just found out that for some reason, there's an issue that forces you to turn your object to Json or encode it before returning it to the client, otherwise it is going to be treated and received as a Pointer.

    A workaround would be:

    publicationImages[i].set("userVote", Parse._encode(vote));
    

    Read more Here