React Native
...
Parse SDK (REST)
Users
Querying Users
9 min
querying users in parse on react native introduction some react native apps need to directly manage your application users or at least be able to list a specific subset of them parse has powerful querying tools and they can also be used for your users in your social media app, for example in this guide, you’ll learn how to use parse query parse query to perform realistic user querying in your react native app using parse js sdk prerequisites to complete this tutorial, you will need a react native app created and connected to back4app goal to build a user querying feature using parse for a react native app 1 understanding the parse query class any parse query operation uses the parse query parse query object type, which will help you retrieve specific data from your database throughout your app it is crucial to know that a parse query parse query will only resolve after calling a retrieve method (like parse query find parse query find or parse query get parse query get ), so a query can be set up and several modifiers can be chained before actually being called to create a new parse query parse query , you need to pass as a parameter the desired parse object parse object subclass, which is the one that will contain your query results an example for this guide use case ( parse user parse user ) can be seen below javascript 1 // this will create your query 2 const parsequery = new parse query(parse user); 3 // the query will resolve only after calling this method 5 const queryresult = await parsequery find();1 // this will create your query 2 const parsequery parse query = new parse query(parse user); 3 // the query will resolve only after calling this method 4 const queryresult \[parse user] = await parsequery find(); you can read more about the parse query parse query class here at the official documentation https //parseplatform org/parse sdk js/api/master/parse query html 2 performing relevant user queries let’s now take a look at some relevant queries that you may need to perform when managing or displaying users in your app first of all, let’s perform a text search query, searching for users whose usernames contain the search value javascript 1 const douserquery = async function () { 2 // this value comes from a state variable 3 const usernamesearchvalue = usernamesearch; 4 // this will create your user query 5 const parsequery = new parse query(parse user); 6 // several query functions can be set to your parse,query, they will 7 // only resolve when calling "find", for example 8 if (usernamesearchvalue !== '') { 9 // "contains" will retrieve users whose username contain the searched value, case sensitive 10 parsequery contains('username', usernamesearchvalue); 11 // 12 // or 13 // 14 // for case insensitive string search, use "matches", that will take into account 15 // an regexp for matching, in this case use only "i", which is the regexp modifier 16 // for case insensitive 17 parsequery matches('username', usernamesearchvalue, 'i'); 18 } 19 // only after calling "find" all query conditions will resolve 20 return await parsequery 21 find() 22 then(async (queriedusers => { 23 // set the query results to an state variable to retrieve it on your jsx 24 // be aware that empty or invalid queries return as an empty array 25 setqueryresults(queriedusers); 26 return true; 27 }) 28 catch((error) => { 29 // error can be caused by lack of internet connection, but in most 30 // cases "find" will return as an empty array on "then" 31 alert alert('error!', error message); 32 setqueryresults(\[]); 33 return false; 34 }); 35 };1 const douserquery = async function () promise\<boolean> { 2 // this value comes from a state variable 3 const usernamesearchvalue string = usernamesearch; 4 // this will create your user query 5 const parsequery parse query = new parse query(parse user); 6 // several query functions can be set to your parse,query, they will 7 // only resolve when calling "find", for example 8 if (usernamesearchvalue !== '') { 9 // "contains" will retrieve users whose username contain the searched value, case sensitive 10 parsequery contains('username', usernamesearchvalue); 11 // 12 // or 13 // 14 // for case insensitive string search, use "matches", that will take into account 15 // an regexp for matching, in this case use only "i", which is the regexp modifier 16 // for case insensitive 17 parsequery matches('username', usernamesearchvalue, 'i'); 18 } 19 // only after calling "find" all query conditions will resolve 20 return await parsequery 21 find() 22 then(async (queriedusers \[parse user]) => { 23 // set the query results to an state variable to retrieve it on your jsx 24 // be aware that empty or invalid queries return as an empty array 25 setqueryresults(queriedusers); 26 return true; 27 }) 28 catch((error object) => { 29 // error can be caused by lack of internet connection, but in most 30 // cases "find" will return as an empty array on "then" 31 alert alert('error!', error message); 32 setqueryresults(\[]); 33 return false; 34 }); 35 }; note that that are at least two different ways to search for a string, each one having its specific applications and advantages in most cases, you will want to use parse query matches parse query matches to ensure case insensitive results and avoid unexpected behavior in your code after performing this query, your user list on your app should be showing something like this in addition to string querying, you can also perform “exact” queries, when you want to retrieve objects that contain an exact value, just as with boolean fields the next example will show how to retrieve users that are verified by email, through the emailverified emailverified field javascript 1 const douserquery = async function () { 2 // this value comes from a state variable 3 const showonlyverifiedvalue boolean = showonlyverified; 4 // this will create your user query 5 const parsequery = new parse query(parse user); 6 // several query functions can be set to your parse,query, they will 7 // only resolve when calling "find", for example 8 if (showonlyverifiedvalue === true) { 9 // "equalto" will retrieve users whose "emailverified" value is exactly "true" 10 parsequery equalto('emailverified', true); 11 } 12 // only after calling "find" all query conditions will resolve 13 return await parsequery 14 find() 15 then(async (queriedusers) => { 16 // set the query results to an state variable to retrieve it on your jsx 17 // be aware that empty or invalid queries return as an empty array 18 setqueryresults(queriedusers); 19 return true; 20 }) 21 catch((error) => { 22 // error can be caused by lack of internet connection, but in most 23 // cases "find" will return as an empty array on "then" 24 alert alert('error!', error message); 25 setqueryresults(\[]); 26 return false; 27 }); 28 };1 const douserquery = async function () promise\<boolean> { 2 // this value comes from a state variable 3 const showonlyverifiedvalue boolean = showonlyverified; 4 // this will create your user query 5 const parsequery parse query = new parse query(parse user); 6 // several query functions can be set to your parse,query, they will 7 // only resolve when calling "find", for example 8 if (showonlyverifiedvalue === true) { 9 // "equalto" will retrieve users whose "emailverified" value is exactly "true" 10 parsequery equalto('emailverified', true); 11 } 12 // only after calling "find" all query conditions will resolve 13 return await parsequery 14 find() 15 then(async (queriedusers \[parse user]) => { 16 // set the query results to an state variable to retrieve it on your jsx 17 // be aware that empty or invalid queries return as an empty array 18 setqueryresults(queriedusers); 19 return true; 20 }) 21 catch((error object) => { 22 // error can be caused by lack of internet connection, but in most 23 // cases "find" will return as an empty array on "then" 24 alert alert('error!', error message); 25 setqueryresults(\[]); 26 return false; 27 }); 28 }; your app should now be updating your user list like this another common example would be to apply orderings to your query this can be done in two ways, either by using parse query ascending/parse query descending parse query ascending/parse query descending or parse query addascending/parse query adddescending parse query addascending/parse query adddescending the first case will override any other ordering and will be the only one that the query will take and the latter will concatenate with existing orderings, making multiple orderings possible javascript 1 const douserquery = async function () { 2 // this value comes from a state variable 3 const orderbyvalue = orderby; 4 // this will create your user query 5 const parsequery = new parse query(parse user); 6 // several query functions can be set to your parse,query, they will 7 // only resolve when calling "find", for example 8 // for list ordering, you can use "addascending" or "adddescending", passing as parameter 9 // which object field should be the one to order by 10 // note that "usernameasc", "usernamedesc" and so on are made up string values applied to a filter in 11 // our example app, so change it by what is suitable to you 12 if (orderbyvalue === 'usernameasc') { 13 parsequery ascending('username'); 14 // 15 // or 16 // 17 parsequery addascending('username'); 18 } else if (orderbyvalue === 'usernamedesc') { 19 parsequery descending('username'); 20 // 21 // or 22 // 23 parsequery adddescending('username'); 24 } else if (orderbyvalue === 'dateasc') { 25 parsequery ascending('createdat'); 26 // 27 // or 28 // 29 parsequery addascending('createdat'); 30 } else if (orderbyvalue === 'datedesc') { 31 parsequery descending('createdat'); 32 // 33 // or 34 // 35 parsequery adddescending('createdat'); 36 } 37 // only after calling "find" all query conditions will resolve 38 return await parsequery 39 find() 40 then(async (queriedusers) => { 41 // set the query results to an state variable to retrieve it on your jsx 42 // be aware that empty or invalid queries return as an empty array 43 setqueryresults(queriedusers); 44 return true; 45 }) 46 catch((error) => { 47 // error can be caused by lack of internet connection, but in most 48 // cases "find" will return as an empty array on "then" 49 alert alert('error!', error message); 50 setqueryresults(\[]); 51 return false; 52 }); 53 };1 const douserquery = async function () promise\<boolean> { 2 // this value comes from a state variable 3 const orderbyvalue string = orderby; 4 // this will create your user query 5 const parsequery parse query = new parse query(parse user); 6 // several query functions can be set to your parse,query, they will 7 // only resolve when calling "find", for example 8 // for list ordering, you can use "addascending" or "adddescending", passing as parameter 9 // which object field should be the one to order by 10 // note that "usernameasc", "usernamedesc" and so on are made up string values applied to a filter in 11 // our example app, so change it by what is suitable to you 12 if (orderbyvalue === 'usernameasc') { 13 parsequery ascending('username'); 14 // 15 // or 16 // 17 parsequery addascending('username'); 18 } else if (orderbyvalue === 'usernamedesc') { 19 parsequery descending('username'); 20 // 21 // or 22 // 23 parsequery adddescending('username'); 24 } else if (orderbyvalue === 'dateasc') { 25 parsequery ascending('createdat'); 26 // 27 // or 28 // 29 parsequery addascending('createdat'); 30 } else if (orderbyvalue === 'datedesc') { 31 parsequery descending('createdat'); 32 // 33 // or 34 // 35 parsequery adddescending('createdat'); 36 } 37 // only after calling "find" all query conditions will resolve 38 return await parsequery 39 find() 40 then(async (queriedusers \[parse user]) => { 41 // set the query results to an state variable to retrieve it on your jsx 42 // be aware that empty or invalid queries return as an empty array 43 setqueryresults(queriedusers); 44 return true; 45 }) 46 catch((error object) => { 47 // error can be caused by lack of internet connection, but in most 48 // cases "find" will return as an empty array on "then" 49 alert alert('error!', error message); 50 setqueryresults(\[]); 51 return false; 52 }); 53 }; your app must now be ordering your queries like this remember that all of the query constraints mentioned above can be chained and performed in a single query, improving your app usability for creating different filters and orderings that will work altogether here is the full code presenting all the query methods used in this guide javascript 1 const douserquery = async function () { 2 // this value comes from a state variable 3 const usernamesearchvalue = usernamesearch; 4 const showonlyverifiedvalue = showonlyverified; 5 const orderbyvalue = orderby; 6 // this will create your user query 7 const parsequery = new parse query(parse user); 8 // several query functions can be set to your parse,query, they will 9 // only resolve when calling "find", for example 10 if (usernamesearchvalue !== '') { 11 // "contains" will retrieve users whose username contain the searched value, case sensitive 12 parsequery contains('username', usernamesearchvalue); 13 // 14 // or 15 // 16 // for case insensitive string search, use "matches", that will take into account 17 // an regexp for matching, in this case use only "i", which is the regexp modifier 18 // for case insensitive 19 parsequery matches('username', usernamesearchvalue, 'i'); 20 } 21 if (showonlyverifiedvalue === true) { 22 // "equalto" will retrieve users whose "emailverified" value is exactly "true" 23 parsequery equalto('emailverified', true); 24 } 25 // for list ordering, you can use "addascending" or "adddescending", passing as parameter 26 // which object field should be the one to order by 27 if (orderbyvalue === 'usernameasc') { 28 parsequery ascending('username'); 29 // 30 // or 31 // 32 parsequery addascending('username'); 33 } else if (orderbyvalue === 'usernamedesc') { 34 parsequery descending('username'); 35 // 36 // or 37 // 38 parsequery adddescending('username'); 39 } else if (orderbyvalue === 'dateasc') { 40 parsequery ascending('createdat'); 41 // 42 // or 43 // 44 parsequery addascending('createdat'); 45 } else if (orderbyvalue === 'datedesc') { 46 parsequery descending('createdat'); 47 // 48 // or 49 // 50 parsequery adddescending('createdat'); 51 } 52 // only after calling "find" all query conditions will resolve 53 return await parsequery 54 find() 55 then(async (queriedusers) => { 56 // set the query results to an state variable to retrieve it on your jsx 57 // be aware that empty or invalid queries return as an empty array 58 setqueryresults(queriedusers); 59 return true; 60 }) 61 catch((error) => { 62 // error can be caused by lack of internet connection, but in most 63 // cases "find" will return as an empty array on "then" 64 alert alert('error!', error message); 65 setqueryresults(\[]); 66 return false; 67 }); 68 };1 const douserquery = async function () promise\<boolean> { 2 // this value comes from a state variable 3 const usernamesearchvalue string = usernamesearch; 4 const showonlyverifiedvalue boolean = showonlyverified; 5 const orderbyvalue string = orderby; 6 // this will create your user query 7 const parsequery parse query = new parse query(parse user); 8 // several query functions can be set to your parse,query, they will 9 // only resolve when calling "find", for example 10 if (usernamesearchvalue !== '') { 11 // "contains" will retrieve users whose username contain the searched value, case sensitive 12 parsequery contains('username', usernamesearchvalue); 13 // 14 // or 15 // 16 // for case insensitive string search, use "matches", that will take into account 17 // an regexp for matching, in this case use only "i", which is the regexp modifier 18 // for case insensitive 19 parsequery matches('username', usernamesearchvalue, 'i'); 20 } 21 if (showonlyverifiedvalue === true) { 22 // "equalto" will retrieve users whose "emailverified" value is exactly "true" 23 parsequery equalto('emailverified', true); 24 } 25 // for list ordering, you can use "addascending" or "adddescending", passing as parameter 26 // which object field should be the one to order by 27 if (orderbyvalue === 'usernameasc') { 28 parsequery ascending('username'); 29 // 30 // or 31 // 32 parsequery addascending('username'); 33 } else if (orderbyvalue === 'usernamedesc') { 34 parsequery descending('username'); 35 // 36 // or 37 // 38 parsequery adddescending('username'); 39 } else if (orderbyvalue === 'dateasc') { 40 parsequery ascending('createdat'); 41 // 42 // or 43 // 44 parsequery addascending('createdat'); 45 } else if (orderbyvalue === 'datedesc') { 46 parsequery descending('createdat'); 47 // 48 // or 49 // 50 parsequery adddescending('createdat'); 51 } 52 // only after calling "find" all query conditions will resolve 53 return await parsequery 54 find() 55 then(async (queriedusers \[parse user]) => { 56 // set the query results to an state variable to retrieve it on your jsx 57 // be aware that empty or invalid queries return as an empty array 58 setqueryresults(queriedusers); 59 return true; 60 }) 61 catch((error object) => { 62 // error can be caused by lack of internet connection, but in most 63 // cases "find" will return as an empty array on "then" 64 alert alert('error!', error message); 65 setqueryresults(\[]); 66 return false; 67 }); 68 }; conclusion at the end of this guide, you learned how to perform queries on parse users on react native in the next guide, we will show you how to save and read data on parse