DEV Community

HarmonyOS
HarmonyOS

Posted on

The `selectContacts` method in ContactsKit is throwing an error.

Read the original article:The 'selectContacts' method in ContactsKit is throwing an error.

Problem Description

Using the selectContacts method in ContactsKit to open the contact selection UI interface, passing in the filter condition (filterClause), a 401 error is thrown during execution. This results in the contact selector failing to display properly, preventing the contact selection operation from being performed.

Background Knowledge

  • contact.selectContacts: A method used to pop up a contact selector in the application, allowing users to select multiple contacts from the system contacts.
  • ContactSelectionOptions: Used to configure the conditions for selecting contacts.
  • ContactSelectionFilter: Used to configure the filtering conditions for contact queries.

Problem Identification

Through the analysis of the partner code, it was discovered that when the selectContacts method is called, the filterClause parameter uses a for loop to pass multiple filterCondition IDs that are identical. This does not meet the expected interface requirements, resulting in a 401 error being returned.

    let arr: contact.FilterOptions[] = []
    for (const id of ['1','2','3','4']) {
      arr.push({filterCondition:contact.FilterCondition.IN, value:id})
    }

    contact.selectContacts({
      isMultiSelect: true,
      maxSelectable: 4,
      filter: {
        filterType:contact.FilterType.DEFAULT_SELECT,
        filterClause:{id:arr}
      },
    },(err: BusinessError, data) => {
      if (err) {
        console.error('selectContact callback, errCode:' + err.code + ', errMessage:' + err.message);
        return;
      }
      console.info(`selectContact callback: success data->${JSON.stringify(data)}`);
    });
Enter fullscreen mode Exit fullscreen mode

Analysis Conclusion

The root cause of the issue is the incorrect input parameter for filterClause.

Modification Suggestion

If the filterCondition is consistent, there is no need to pass multiple ID values. Please refer to the following code for modification:

    contact.selectContacts({
      isMultiSelect: true,
      maxSelectable: 4,
      filter: {
        filterType:contact.FilterType.DEFAULT_SELECT,
        filterClause:{id:[{filterCondition:contact.FilterCondition.IN, value:['1','2','3','4']}]}
      },
    },(err: BusinessError, data) => {
      if (err) {
        console.error('selectContact callback, errCode:' + err.code + ', errMessage:' + err.message);
        return;
      }
      console.info(`selectContact callback: success data->${JSON.stringify(data)}`);
    });
Enter fullscreen mode Exit fullscreen mode

Written by Mehmet Karaaslan

Top comments (0)