hewa
hewa

Reputation: 73

How to properly query data in AWS Amplify

I run the following function in my login screen to see if there is an user in the database and if not to run signup, But this function doesn't work correctly sometimes. I want to know whether my function is correct.

const Function1 = async () => {
const user = await DataStore.query(User, d => d.Phonenumb("eq", phoneNumb))

if(user.length !== 0){
      signIn();
   } else if (user.length === 0){
      signup();
   } else {
      return
   }
}

useEffect(() => {
  Function1();
 }, []);

Upvotes: 0

Views: 301

Answers (1)

Dave
Dave

Reputation: 7717

The phoneNumb value used by Function1 should be added to the your useEffect's dependency list to re-run should that change.

Where does phoneNumb come from? If you have it, then you've seen a user before on that device and may want to just show signIn. If you don't, then signUp.

Using the predicate d => d.Phonenumb("eq", phoneNumb) will search through all users and can return multiple users. I think it'd be better if you could use the User.id instead so you can just pass that in and get that exact user (or not).

Also Function1 is async, so you should await it.

Upvotes: 1

Related Questions