Ad

Retrieving Information From Pointer Field

- 1 answer

I'm trying to retrieve a field from a Pointer as suggested on this post but I'm always getting undefined.

This is what my table Publication looks like:

  • userId ->User
  • subCategoryId ->SubCategory
  • title
  • description

And my SubCategory table:

  • categoryId ->Category
  • name
  • isActive

And this is my attempt (I only have one row on the table):

var user = Parse.User.current();
var User = Parse.Object.extend("User");
var userQuery = new Parse.Query(User);
userQuery.equalTo("objectId", user.id);

var Publication = Parse.Object.extend("Publication");
var publicationQuery = new Parse.Query(Publication);
publicationQuery.include("subCategoryId");
publicationQuery.matchesQuery("userId", userQuery);        
publicationQuery.find({
    success: function(publications) {
        console.log(publications[0].get("title"));
        // This one returns undefined
        console.log(publications[0].get("subCategoryId"));
    }, error: function(error) {
        // Nothing here as suggested by @adolfosrs
        console.log(error);
    }
});

What I need is:

publications[0].get("subCategoryId").get("name");

But obviously the latter throws:

Uncaught TypeError: Cannot read property 'get' of undefined
Ad

Answer

If you have Pointers in your parse database like the following, there is no need on working with the objectIds.

enter image description here

The data that you should have is something like this:

Publication:

  • user (Pointer <_User>)
  • subCategory (Pointer <SubCategory>)
  • title (String)
  • description (String)

SubCategory:

  • category (Pointer <Category>)
  • name (String)
  • isActive (boolean)

So if you are saving the data as expected the following must work:

var currentUser = Parse.User.current();
var Publication = Parse.Object.extend("Publication");
var publicationQuery = new Parse.Query(Publication);
publicationQuery.equalTo("user", currentUser); 
publicationQuery.include("subCategory");       
publicationQuery.find({
    success: function(publications) {
        console.log(publications[0].get("title"));
        // This one returns undefined
        console.log(publications[0].get("subCategory").get("name");
    }
});
Ad
source: stackoverflow.com
Ad