来自 JS 的错误调用:字段大小不同 [[8,39],[4,0]

the*_*lim 5 android android-permissions react-native react-native-contacts react-hooks

我想在我的 AVD 上显示联系人列表,但我遇到了错误(我尝试链接包但它什么也没做):

我的代码:

    const [contact, setContact] = useState([]);
  
    useEffect(() => {
      PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
        {
          'title': 'Contacts',
          'message': 'This app would like to view your contacts.'
        }
      ).then(() => {
        Contacts.getAll((err, contacts) => {
          if (err === 'denied'){
            // error
          } else {
            // contacts returned in Array
            setContact(contacts);
            console.log(contact);
          }
        })
      })
      .catch((err)=> {
          console.log(err);
      })
    }, []);
Run Code Online (Sandbox Code Playgroud)

错误 :

在此处输入图片说明

我到处寻找解决方案,但没有关于这个问题,谢谢你提前帮助我。

the*_*tar 11

API 已在版本 6 中更新。从回调(版本 5 使用回调)更改为 promise 对我有用。我的意思是改变 -

Contacts.getAll((err, contacts) => { });
Run Code Online (Sandbox Code Playgroud)

到 -

Contacts.getAll()
    .then((contacts) => {
      // work with contacts
    })
    .catch((e) => { //handle error })
Run Code Online (Sandbox Code Playgroud)

  • 这不是库的问题,因为文档清楚地提到了这种用法。您还可以检查 getAll 方法的类型。https://github.com/morenoh149/react-native-contacts/blob/master/index.d.ts (2认同)