Query on pointer in parse.com Objects in Javascript

Y M picture Y M · Nov 14, 2014 · Viewed 7.7k times · Source

I have a Class of Company which has User pointers. The query I want on Company class is like this:

Retrieve Company rows where User object has a name equal to 'ABC'

So, how should I form this query ?

var Company = Parse.Object.extend("Company");
var query = Parse.Query(Company);
query.include("User");

query.equalTo("name")       ????

Is it possible to write such a request in a single query ? Thanks.

Answer

jeffdill2 picture jeffdill2 · Dec 1, 2014

You'll need to query for the User first based on the name of "ABC". Then in the success callback of that query, do the query on your Company table using the objectId returned from the User query. Something like this:

var userQuery = Parse.Query('_User');
var companyQuery = Parse.Query('Company');

userQuery.equalTo('name', 'ABC');

userQuery.find({
  success: function(user) {
    var userPointer = {
      __type: 'Pointer',
      className: '_User',
      objectId: user.id
    }

    companyQuery.equalTo('user', userPointer);

    companyQuery.find({
      success: function(company) {
        // WHATEVER
      },
      error: function() {
      }
    });
  },
  error: function() {
  }
});