React Native
...
Users
การค้นหาผู้ใช้ใน Parse SDK สำหรับ React Native
8 นาที
การค้นหาผู้ใช้ใน parse บน react native บทนำ แอป react native บางตัวจำเป็นต้องจัดการผู้ใช้แอปพลิเคชันของคุณโดยตรงหรืออย่างน้อยก็สามารถแสดงรายการกลุ่มเฉพาะของพวกเขาได้ parse มีเครื่องมือการค้นหาที่ทรงพลังและสามารถใช้สำหรับผู้ใช้ในแอปโซเชียลมีเดียของคุณได้ ตัวอย่างเช่น ในคู่มือนี้ คุณจะได้เรียนรู้วิธีการใช้ parse query parse query เพื่อทำการค้นหาผู้ใช้ที่สมจริงในแอป react native ของคุณโดยใช้ parse js sdk ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ คุณจะต้องมี แอป react native ที่สร้างขึ้นและ https //www back4app com/docs/react native/parse sdk/react native sdk เป้าหมาย เพื่อสร้างฟีเจอร์การค้นหาผู้ใช้โดยใช้ parse สำหรับแอป react native 1 เข้าใจคลาส parse query การดำเนินการค้นหาของ parse ใด ๆ จะใช้ parse query parse query ซึ่งจะช่วยให้คุณดึงข้อมูลเฉพาะจากฐานข้อมูลของคุณตลอดทั้งแอปของคุณ เป็นสิ่งสำคัญที่ต้องรู้ว่า parse query parse query จะถูกแก้ไขเฉพาะหลังจากเรียกใช้วิธีการดึงข้อมูล (เช่น parse query find parse query find หรือ parse query get parse query get ), ดังนั้นการค้นหาสามารถตั้งค่าและมีการปรับแต่งหลายอย่างที่สามารถเชื่อมโยงกันได้ก่อนที่จะถูกเรียกใช้งานจริง ในการสร้าง parse query parse query ใหม่ คุณต้องส่งพารามิเตอร์เป็น parse object parse object ซึ่งเป็นคลาสที่จะเก็บผลลัพธ์การค้นหาของคุณ ตัวอย่างสำหรับกรณีการใช้งานในคู่มือนี้ ( parse user parse user ) สามารถดูได้ด้านล่าง 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(); คุณสามารถอ่านเพิ่มเติมเกี่ยวกับ parse query parse query คลาส https //parseplatform org/parse sdk js/api/master/parse query html 2 การดำเนินการค้นหาผู้ใช้ที่เกี่ยวข้อง ตอนนี้เรามาดูคำค้นหาที่เกี่ยวข้องบางอย่างที่คุณอาจต้องดำเนินการเมื่อจัดการหรือแสดงผู้ใช้ในแอปของคุณกันก่อนเลย มาทำการค้นหาข้อความ โดยค้นหาผู้ใช้ที่มีชื่อผู้ใช้ประกอบด้วยค่าการค้นหา 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 }; โปรดทราบว่ามีอย่างน้อยสองวิธีที่แตกต่างกันในการค้นหาสตริง โดยแต่ละวิธีมีการใช้งานและข้อดีเฉพาะตัว ในกรณีส่วนใหญ่ คุณจะต้องการใช้ parse query matches parse query matches เพื่อให้แน่ใจว่าผลลัพธ์ไม่คำนึงถึงตัวพิมพ์ใหญ่และหลีกเลี่ยงพฤติกรรมที่ไม่คาดคิดในโค้ดของคุณ หลังจากทำการค้นหานี้ รายชื่อผู้ใช้ในแอปของคุณควรแสดงอะไรบางอย่างเช่นนี้ นอกจากการค้นหาสตริงแล้ว คุณยังสามารถทำการค้นหา “exact” ได้เมื่อคุณต้องการดึงวัตถุที่มีค่าที่แน่นอน เช่นเดียวกับฟิลด์ boolean ตัวอย่างถัดไปจะแสดงวิธีการดึงผู้ใช้ที่ได้รับการตรวจสอบโดยอีเมล ผ่านทาง emailverified emailverified ฟิลด์ 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 }; แอปของคุณควรอัปเดตรายชื่อผู้ใช้ของคุณแบบนี้ อีกตัวอย่างที่พบบ่อยคือการใช้การจัดลำดับในคำค้นของคุณ ซึ่งสามารถทำได้สองวิธี คือการใช้ parse query ascending/parse query descending parse query ascending/parse query descending หรือ parse query addascending/parse query adddescending parse query addascending/parse query adddescending กรณีแรกจะทำให้การจัดลำดับอื่นๆ ถูกแทนที่และจะเป็นเพียงลำดับเดียวที่คำค้นจะใช้ และกรณีหลังจะรวมเข้ากับการจัดลำดับที่มีอยู่ ทำให้สามารถมีการจัดลำดับหลายๆ อย่างได้ 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 }; แอปของคุณต้องสั่งการค้นหาของคุณแบบนี้ โปรดจำไว้ว่าข้อจำกัดของการค้นหาทั้งหมดที่กล่าวถึงข้างต้นสามารถเชื่อมโยงและดำเนินการในคำค้นหาเดียว ซึ่งจะช่วยปรับปรุงการใช้งานแอปของคุณในการสร้างตัวกรองและการจัดเรียงที่แตกต่างกันซึ่งจะทำงานร่วมกันได้ นี่คือรหัสทั้งหมดที่นำเสนอวิธีการค้นหาทั้งหมดที่ใช้ในคู่มือนี้ 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 }; บทสรุป เมื่อสิ้นสุดคู่มือนี้ คุณได้เรียนรู้วิธีการทำการค้นหาผู้ใช้ parse บน react native ในคู่มือต่อไป เราจะแสดงให้คุณเห็นวิธีการบันทึกและอ่านข้อมูลบน parse