Duplicate emails when searching Contacts

Found a problem with this code, which finds a contact’s unique ID based on an email. It works fine until two or more contacts share the same email on file, which I believe is permitted in the case of contacts but not site members.

So I either need to find a way to sort which is the correct user in the case of a duplicate email, or query a different email field altogether. But where is the data for “info.emails.email” stored? CMS/Full Data only shows the login emails of site members; is there a separate CMS collection for contacts?

I also looked in the Contacts section of Dashboard but the two contacts that show up as duplicates in the email query appear there with separate emails. I couldn’t see any hidden email fields or fields for a contact’s alternate/secondary email. In fact, no sign of any duplicate email until I run the code.

const queryResults = await contacts.queryContacts()
    .eq('info.emails.email', emailToFind)
    .find ({suppressAuth: true});
  const contactsWithEmail = queryResults.items;

  if (contactsWithEmail.length >= 1) {
    contactID = contactsWithEmail[0]._id;
    console.log('Found contact info for client: ' + emailToFind + " " + contactID);
  } else {
     // Handle when no contacts are found
  }

The queryResults will contain an array of items that each have a.info.emails property which is an array with all the matching contact’s emails (up to 50 emails). You can then use these results to determine all of the returned contact’s emails and deduplicate as needed.

The code as currently written only handles the first item in the array of items (ie. item[0]).

Can read more here: https://www.wix.com/velo/reference/wix-crm-backend/contacts/contactsqueryresult/items

Thanks. I realize the array of items is not actually the emails, but rather the unique ID of all contacts linked to the email queried, some as primary and others as alternate emails.

In my case, the query was returning the IDs of contacts on file with the queried email as an alternate email, and the Triggered email ended up not being sent to the email that was queried, but rather the primary email of a contact who had listed the queried email as an alternate.

If Triggered emails only sends the email to the “primary” email of the contact ID supplied then instead of querying .info.emails would it not be better to query primaryInfo.emails ?

Alternatively, how can you send a Triggered email to a specific alternate email?