Search code examples
javascriptparse-platform

Parse.com Query containedIn Array of Pointers


Original Question

I'm trying to query all instances where the ID inside of allFriends matches the ID of Creator. allFriends is an array of IDs from an earlier query. What is the proper syntax for containedIn with an array of pointers.

allFriends:

["1EGomEr0Tk", "iMJbVrPfGG", "DVOwabZkaQ", "zhvd8mAdhl"]

Code:

query.containedIn("Creator", {
                __type: "Pointer",
                className: "User",
                objectId: allFriends
            })

Error:

code: 102
error: "$in requires an array"

Update #1

I've found that I need to convert the allFriends array of IDs into a pointer map. However _.map is being computed as undefined.

Code:

var pointers = _.map(allFriends, function(item_id) {
            var pointer = new Parse.Object("allFriends");
            pointer.id = item_id;
            return pointer;
            });
            query.containedIn("Creator", pointers);

Error:

Uncaught ReferenceError: map is not defined

Solution

  • If your "Creator" column is of type Pointer<_User> and your allFriends array is an array of IDs for Users, then this is what you need to do:

    Create an array of Pointer<_User> objects from allFriends:

    var pointers = _.map(allFriends, function(friendId) {
      var pointer = new Parse.User();
      pointer.id = friendId;
      return pointer;
    });
    

    Once you have an array of Pointer<_User> you can then compare your column:

    query.containedIn('Creator', pointers);
    

    You were very close, it was just a matter of creating the right kind of pointers.