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
but i want to search a string in a attribute
Meaning?
for example i want to start searching for either name for phone number
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
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
its custom collection
Ohh Then just change the code to query the database instead
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
}
im trying Query.search()
Updated it It work the same
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
Recommended threads
- Invalid document structure: missing requ...
I just pick up my code that's working a week ago, and now I got this error: ``` code: 400, type: 'document_invalid_structure', response: { message: 'Inv...
- custom domain with CloudFlare
Hi all, it seems that CloudFlare has blocked cross-domain CNAME link which made my app hostname which is in CloudFlare, unable to create a CNAME pointing to clo...
- Custom emails
What happen if I use a third party email provider to customize my emails and my plan run out of emails/month? Appwrite emails are used as fallback sending emai...