react-native - 尝试在 React Native 中使用文档选择器时无法读取未定义的属性(读取 'pick')

标签 react-native uidocumentpicker

我正在尝试为我的 react-native 应用程序使用 document picker。我尝试使用此命令来安装文档选择器:npm i react-native-document-picker。编写一些代码后,我首先在网络浏览器上打开我的应用程序。但是当我尝试单击选择文件的按钮时,总是会发生此错误。有人能解决这个问题吗?

非常感谢大家。下面是我的代码示例

    import React from 'react';
    import {
      View,
      Button,
    } from 'react-native';
    import DocumentPicker from 'react-native-document-picker';
    
    export default function App() {
      const openDocument = async () => {
        try {
          const res = await DocumentPicker.pick({
            type: [DocumentPicker.types.allFiles],
          });
          console.log(
            res.uri,
            res.type, // mime type
            res.name,
            res.size
          );
        } catch (err) {
          if (DocumentPicker.isCancel(err)) {
            // User cancelled the picker, exit any dialogs or menus and move on
          } else {
            throw err;
          }
        }
      };
      return (
        <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
          <Button title="Choose file" onPress={() => openDocument()} />
        </View>
      );
    }

最佳答案

尝试

console.log(res);

如果它不是 NULL,你必须选择一个单一的文件,它会将 JSON 安全地保存到一个数组中。

try {
          const res = await DocumentPicker.pickSingle({
            type: [DocumentPicker.types.allFiles],
          });
          console.log(
            res.uri,
            res.type, // mime type
            res.name,
            res.size
          );
        }

如果你选择 DocumentPicker.pick 你必须做一个 for-loop

try {
          const res = await DocumentPicker.pickSingle({
            type: [DocumentPicker.types.allFiles],
          });
        for(i in res){
          console.log(
            i.uri,
            i.type, // mime type
            i.name,
            i.size
           );}
        }

关于react-native - 尝试在 React Native 中使用文档选择器时无法读取未定义的属性(读取 'pick'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69594956/

相关文章:

reactjs - 由于 track-player 和 react-native-video 中的 exo 播放器版本冲突,react-native-track-player 崩溃

javascript - 如何将 Prop 从父级传递给子级再到子级的子级?

image - 我如何在 React Native 中实际使用 Image.capInsets?

reactjs - React Native Paper 水平和垂直对齐卡片内容

javascript - 单击按钮时想要在 saveInfo() 函数内执行 disNone() 函数