[SOLVED] Perform Query on Array Key for Class User?

In my app I save a set of skill tags in an array assigned to the User Class. Iā€™m performing a cloud function that queries Users for a matching value in the array ā€œskillSetā€ after a button is clicked for the corresponding tag.

image

So click ā€œDeveloperā€ tag and then search for Userā€™s where ā€œDeveloperā€ is a value in the array ā€œskillSetā€

image

How can we query a value that is within this array? I read the docs and canā€™t find any instructions for a query function to find a value saved in an array that is a key of an object such as I have here. I tried query.containedIn(ā€œskillSetā€, ā€œDeveloperā€) and query.equalTo(ā€œskillSetā€, ā€œDeveloperā€) but these donā€™t appear to be valid for an array and return ā€œinvalid functionā€

My cloud function is currently the following, however itā€™s not producing any results:

Moralis.Cloud.define("searchUsersByTag", async (request) => {
  const Profiles = Moralis.Object.extend("User");
  let query = new Parse.Query(Profiles);
  query.exists('skillSet');  
  query.select('username', 'userLocation', 'bio', 'profilePic', 'createdAt',"skillSet");
  query.descending('createdAt');
  const queryResults = await query.find({useMasterKey:true});
  const results = [];  
  for (let i = 0; i < queryResults.length; ++i) {
    if (queryResults[i].attributes.skillSet.includes(request.params.skillTag)) {
      if(typeof queryResults[i].attributes.profilePic !== 'undefined'){    
        results.push({
          "username": queryResults[i].attributes.username,
          "location": queryResults[i].attributes.userLocation,   
          "bio": queryResults[i].attributes.bio,       
          "profilePic": queryResults[i].attributes.profilePic._url,
          "skillSet": queryResults[i].attributes.skillSet,
        });
      };
    }
  }
  return results;
});

Thanks for the help!

1 Like

The issue was that I was passing the value in the array, rather than the array itself. Thanks, this link helped me catch that!