How to Query Based on a Pointer on Parse Server

Parse is the popular backend as a service platform that Facebook owned but recently shutdown. However, before doing so, they released Parse Server, an open source version of the Parse backend that can be deployed to any infrastructure that can run Node.js. I have used Parse extensively in the past on many projects and have recently migrated to Parse Server. I've had a great experience using it and the community around it is great.

I recently got a question from a good friend of mine on how to run a query on Parse Server based on a pointer. Having done that many times in the past couple years, this was second-nature to me but I had forgotten how much trouble I had the first time I tried doing such a query. His question reminded me that the documentation for this specific issue is poor, so I decided to write this little short post to help out others in search of an answer.

Imagine you have a Visit object that has a pointer to User. You would think that writing the query like this would work, but it doesn't:

// This does not work
var visitQuery = new Parse.Query('Visit');
visitQuery.equalTo('user', userId);
return visitQuery.find();

Instead, you need to create a pointer object and use it in the query.

var visitQuery = new Parse.Query('Visit');
visitQuery.equalTo('user', { "__type": "Pointer", "className": "_User", "objectId": userId });
return visitQuery.find();

In this case, the className is _User since it's a Parse-defined class (notice the leading underscore). Otherwise, className is simply the name of your class (e.g. Visit).

I hope this simple little trick helps a few developers out there as much as it helped my friend.

Wissam Abirached

Wissam Abirached