Rank 4: Virtual Machine
Hi i want a search a user either by his name or phone number... How do i do that using Query.equal?
Votes
0
Replies
21
Participants
Unknown
Messages
22
Rank 4: Virtual Machine
Hi i want a search a user either by his name or phone number... How do i do that using Query.equal?
Searching of user can work only with the Users API with Server side SDK.
And that means you'll need either deploy Appwrite function or in any backend code you're using.
So, this function will let you to run the query against the users
https://appwrite.io/docs/server/users?sdk=nodejs-default#usersList
And the final code can be something similar to this (JavaScript snippet)
client .setEndpoint('https://cloud.appwrite.io/v1') // Your API Endpoint .setProject('5df5acd0d48c2') // Your project ID .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key;
try { const users = await users.list([ Query.equal("email", ["john@appwrite.io"]), Query.equal("phone", ["+1234567890"]), ]);
} catch (e) { // TODO: handle error}All query-able attributes can be found here in the user model
https://appwrite.io/docs/models/user
Rank 4: Virtual Machine
but i want to search a string in a attribute
Meaning?
Rank 4: Virtual Machine
for example i want to start searching for either name for phone number
Rank 4: Virtual Machine
it should work like Query.search("name", search) **OR** Query.search("phoneNumber", search)
Mmm. Got you
There is no option from what I know to do in Appwrite
Rank 4: Virtual Machine
hmm 😦
I think there some issue regards this one.
appwrite doesn't support OR operation in that case.
But the there is a workaround making two API calls
let searchUser = null;
try { // search by email searchUser = await users.list([ Query.equal("email", ["john@appwrite.io"]), });
// or search by phone if(!searchUser || searchUser.documents.length === 0) { searchUser = await users.list([ Query.equal("phone", ["+1234567890"]) }} catch (e) { // handle error}do you specifically want to search through Users or your custom Collection?
You can upvote this issue
Rank 4: Virtual Machine
its custom collection
Ohh
Then just change the code to query the database instead
Rank 4: Virtual Machine
yeah but thats not an efficient approach i guess
client .setEndpoint('https://your-server-domain-or-ip/v1') .setProject('5df5acd0d48c2');
try { const users = await databases.listDocuments('[DATABASE_ID]', '[COLLECTION_ID]', [ Query.search('email', 'john@appwrite.io'), Query.search('phone', '+1234567890') ] );
} catch (e) { // TODO: handle error}Rank 4: Virtual Machine
im trying Query.search()
Updated it
It work the same
Rank 4: Virtual Machine
as i type the search box.. it should dynamically search name and phone number
This would be the related feature request then
as a workaround, you can create search attribute that has all the text you want to search against