Advice on finding a user in Sanity database by email or phone number
9 replies
Last updated: Jul 5, 2022
S
Hello, I want to find an user from sanity database by email or phone number. I ended up with an error after I run below code const user = await client.fetch( `*[_type == "user" && email == $email || phone == $phone][0]`, { email: email, phone:phone } );
Jul 4, 2022, 4:20 AM
H
`*[_type == "user" && email == $email || phone == $phone][0]`, { email, phone } );
Jul 4, 2022, 6:19 AM
H
In GROQ, you don't need to declare both the key and value if they are the same. If you want to rename the key, or define an attribute with a key that the schema does not have, you need to put quotes around the key like so:
Jul 4, 2022, 6:20 AM
H
I deleted my messages realizing I didn't read your code properly.
Jul 4, 2022, 6:21 AM
H
What error are you getting?
Jul 4, 2022, 6:21 AM
K
Yes, what error?
Jul 4, 2022, 7:32 AM
K
You might want to clarify your expression with parentheses though:
*[_type == "user" && (email == $email || phone == $phone)][0]
Jul 4, 2022, 7:33 AM
S
Hello All , Thanks for your help.Actually I have only one input html field that carries email and phone both i.e "userInput". TEST CODE-> To test I have hard-coded email and phone and then pass it to below code Followings(email & phone) are available in database const email = "<mailto:test@gmail.com|test@gmail.com>" const phone = "8009065120" const user = await client.fetch( `*[_type == "user" && (email == "${email}" || phone == "${phone}")][0]` ); with above hard-coded values it fetches user for both email and phone. So above api call successful because I handled phone and email separately. ACTUAL CODE-> In actual code i am handling email and phone via single html input that is "userInput". below api call goes through for email input(when userInput carries email) but below api call fails for phone number. Error message -> user is null; I realized that issue is here -> (email == "${userInput}" || phone == "${userInput}") when userInput carries phone number it still tries to find user based on email. const user = await client.fetch( `*[_type == "user" && (email == "${userInput}" || phone == "${userInput}")][0]` ); if userInput is email , it fetches user if userInput is phone, it fails and gives user as null if(user){ ...rest of code for login } LOOKING FOR AN ADVISE ON BELOW -> Do I need to implement some validation like below? const ifEmail = userInput.includes("@"); const ifPhone = /^\d+$/.test(userInput) let user; if(ifEmail){ user = await client.fetch( `*[_type == "user" && (email == "${email}"][0]` ); } if(ifPhone){ user = await client.fetch( `*[_type == "user" && (phone == "${phone}"][0]` ); }
Jul 5, 2022, 5:18 AM
S
Hello All , Thanks for your reply. Actually I have only one input html field that carries email and phone both i.e "userInput". TEST CODE-> To test I have hard-coded email and phone and then pass it to below code Followings(email & phone) are available in database const email = "<mailto:test@gmail.com|test@gmail.com>" const phone = "8009065120" const user = await client.fetch( `*[_type == "user" && (email == "${email}" || phone == "${phone}")][0]` ); with above hard-coded values it fetches user for both email and phone. So above api call successful may be because I handled phone and email separately. ACTUAL CODE-> In actual code i am handling email and phone via single html input that is "userInput". below api call goes through for email input(when userInput carries email) but below api call fails for phone number. Error message -> user is null; I realized that issue is here -> (email == "${userInput}" || phone == "${userInput}") when userInput carries phone number it could notfind user based on phone. In user model phone is type number. const user = await client.fetch( `*[_type == "user" && (email == "${userInput}" || phone == "${userInput}")][0]` ); if userInput is email , it fetches user if userInput is phone, it fails and gives user as null if(user){ ...rest of code for login } user model -> title: "User", type: "document", fields: [ { name: "name", title: "Name", type: "string", }, { name: "phone", title: "Phone", type: "number", }, { name: "email", title: "Email", type: "string", }, { name: "password", title: "Password", type: "string", }, ], };
Jul 5, 2022, 5:45 AM
S
Hello All , Thanks for your help.Earlier the error was user-> null while tried to find user by phone. Now the code is working for both email and phone The api is working below way const user = await client.fetch( `*[_type == "user" && (email == $email || phone == $phone)][0]`,{ email: email, phone: Number(phone) } );
Jul 5, 2022, 6:25 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.