React Native
...
Users
Interrogazioni Utenti Parse su React Native con JS SDK
9 min
interrogare gli utenti in parse su react native introduzione alcune app react native devono gestire direttamente gli utenti della tua applicazione o almeno essere in grado di elencare un sottoinsieme specifico di essi parse ha potenti strumenti di interrogazione e possono essere utilizzati anche per i tuoi utenti nella tua app di social media, ad esempio in questa guida, imparerai come utilizzare parse query parse query per eseguire interrogazioni realistiche sugli utenti nella tua app react native utilizzando il parse js sdk prerequisiti per completare questo tutorial, avrai bisogno di un'app react native creata e collegata a back4app obiettivo costruire una funzionalità di interrogazione degli utenti utilizzando parse per un'app react native 1 comprendere la classe parse query qualsiasi operazione di query di parse utilizza il parse query parse query tipo di oggetto, che ti aiuterà a recuperare dati specifici dal tuo database in tutta l'app è fondamentale sapere che un parse query parse query si risolverà solo dopo aver chiamato un metodo di recupero (come parse query find parse query find o parse query get parse query get ), quindi una query può essere impostata e diversi modificatori possono essere concatenati prima di essere effettivamente chiamati per creare un nuovo parse query parse query , devi passare come parametro la parse object parse object sottoclasse desiderata, che è quella che conterrà i risultati della tua query un esempio per questo caso d'uso della guida ( parse user parse user ) può essere visto qui sotto 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(); puoi leggere di più sulla parse query parse query classe qui nella documentazione ufficiale https //parseplatform org/parse sdk js/api/master/parse query html 2 eseguire query utente rilevanti diamo ora un'occhiata ad alcune query rilevanti che potresti dover eseguire quando gestisci o visualizzi gli utenti nella tua app prima di tutto, eseguiamo una query di ricerca testuale, cercando utenti i cui nomi utente contengono il valore di ricerca 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 }; nota che ci sono almeno due modi diversi per cercare una stringa, ognuno con le proprie applicazioni e vantaggi specifici nella maggior parte dei casi, vorrai usare parse query matches parse query matches per garantire risultati senza distinzione tra maiuscole e minuscole e per evitare comportamenti imprevisti nel tuo codice dopo aver eseguito questa query, la tua lista utenti sulla tua app dovrebbe mostrare qualcosa di simile a questo oltre alla ricerca di stringhe, puoi anche eseguire query “esatte”, quando vuoi recuperare oggetti che contengono un valore esatto, proprio come con i campi booleani il prossimo esempio mostrerà come recuperare utenti che sono verificati tramite email, attraverso il emailverified emailverified campo 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 }; la tua app dovrebbe ora aggiornare la tua lista utenti in questo modo un altro esempio comune sarebbe applicare ordinamenti alla tua query questo può essere fatto in due modi, o utilizzando parse query ascending/parse query descending parse query ascending/parse query descending o parse query addascending/parse query adddescending parse query addascending/parse query adddescending il primo caso sovrascriverà qualsiasi altro ordinamento e sarà l'unico che la query prenderà in considerazione, mentre il secondo si concatenarà con gli ordinamenti esistenti, rendendo possibili più ordinamenti 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 }; la tua app deve ora ordinare le tue query in questo modo ricorda che tutti i vincoli di query menzionati sopra possono essere concatenati e eseguiti in un'unica query, migliorando l'usabilità della tua app per creare diversi filtri e ordinamenti che funzioneranno insieme ecco il codice completo che presenta tutti i metodi di query utilizzati in questa guida 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 }; conclusione alla fine di questa guida, hai imparato come eseguire query sugli utenti di parse su react native nella prossima guida, ti mostreremo come salvare e leggere dati su parse