Search code examples
objective-carraysstringparse-platformpfquery

Parse check if email exists query


I'm trying to get a query of all the emails from Parse and check if it is the same as one that a user inputs.

PFQuery *query = [PFUser query];
[query whereKey:@"email" equalTo:emailerL];
NSArray *emailers = [query findObjects];
if ([emailers containsObject:emailerL]) {
    [PFUser requestPasswordResetForEmailInBackground:emailerL];

    [self.navigationController popToRootViewControllerAnimated:YES];
}
else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please try again" message:@"Email does not exist" delegate:self cancelButtonTitle:@"Gotcha" otherButtonTitles:nil];
    [alert show];
}

I don't know why the if statement isn't working. (emailers is an array and emailerL is a string).

I guess I just need help with comparing a string to data in an array.


Solution

  • To check for at least one record that matches a query, use getFirstObject instead, it will return nil if it doesn't find a match and will be a lot quicker.

    PFQuery *query = [PFUser query];
    [query whereKey:@"email" equalTo:emailerL];
    
    if ([query getFirstObject]) {
        [PFUser requestParrwordResetForEmailInBackground:emailerL];
        // ... etc ...
    

    Of course, you should really be using the "in background" version, but this is your first step to try.