César
César

Reputation: 10119

How to get a record with an objectId for the Pointer field?

Let's say I have the objectId of a record from a Category class and I need to fetch all the subcategories (from SubCategory class) that belong to that category. SubCategory.categoryId is a Pointer field to Category. If I do this:

// State

.state('app.create-chambba', {
  cache: false,
  url: "/services/create-chambba/:categoryId",
  views: {
    "menuContent": {
      templateUrl: "views/app/services/create_chambba.html",
      controller: "CreateChambbaCtrl"
    }
  }
})

// Controller

var SubCategory = Parse.Object.extend("SubCategory");
var query = new Parse.Query(SubCategory);
query.equalTo("isActive", true);
query.equalTo("categoryId", $stateParams.categoryId); // categoryId is "aOPHM46mtR" 
query.ascending("name");
query.find(...);

I get:

message: "pointer field categoryId needs a pointer value"

So I ended up doing:

var Category = Parse.Object.extend("Category");
var queryCategory = new Parse.Query(Category);
queryCategory.equalTo("objectId", $stateParams.categoryId);
queryCategory.first({
    success: function(category) {
        var SubCategory = Parse.Object.extend("SubCategory");
        var query = new Parse.Query(SubCategory);
        query.equalTo("isActive", true);
        query.equalTo("categoryId", category);
        query.ascending("name");
        query.find(..);
    }
});

Isn't that extra work? Is there a better way?

Upvotes: 1

Views: 57

Answers (1)

Alon
Alon

Reputation: 2929

Create a new object of the category and put the id of this object the id that you search.

  var category = new Category();  
  category.id=$stateParams.categoryId; 

  ....

and use the category object as a pointer .

  query.equalTo("categoryId", catagory); 

this is little hack but it should solve the problem.

Upvotes: 2

Related Questions