hufans / react-native-alioss

aliyun oss for react native
MIT License
16 stars 8 forks source link

使用的时候会报 TypeError: null is not an object #9

Open eugle opened 2 years ago

eugle commented 2 years ago

在taro.js 框架中使用,模板是react native 调用任何api,都会报

TypeError: null is not an object (evaluating 'AliOss.enableDevMode')

比如这样

//文件上传
  function upload() {
    console.log("准备上传")

    //open log
    console.log(AliyunOSS);
    if (AliyunOSS) {
      AliyunOSS.enableDevMode() //报错

      // defalut configraiton
      const configuration = {
        maxRetryCount: 3,
        timeoutIntervalForRequest: 30,
        timeoutIntervalForResource: 24 * 60 * 60
      };

      const key = '1'
      const secret = '2'
      const endpoint = "https://oss-cn-shenzhen.aliyuncs.com"
      AliyunOSS.initWithPlainTextAccessKey(key, secret, endpoint, configuration);
      AliyunOSS.asyncUpload('jimuku', 'app', album[0]).then(res => {
        console.log(res);
      }).catch(err => {
        console.log(err);
      })
    }

  }
eugle commented 2 years ago

是不是方法不对,有没有示例demo,谢谢

eugle commented 2 years ago

源码中找到的示范

export default function App() {
  const [result, setResult] = React.useState<number | undefined>();

  React.useEffect(() => {
    // AliyunOSS.initWithSecurityToken();
    // AliyunOSS.asyncUpload();
  }, []);

然后这样写

export default function Publish() {

  useEffect(() => {
    const configuration = {
      maxRetryCount: 3,
      timeoutIntervalForRequest: 30,
      timeoutIntervalForResource: 24 * 60 * 60
    };

    const key = ''
    const secret = ''
    const endpoint = "https://oss-cn-shenzhen.aliyuncs.com"
    AliyunOSS.initWithSecurityToken(key, secret, endpoint, configuration);
  }, [])

错误如下:

 ERROR  TypeError: null is not an object (evaluating 'AliOss.initWithSecurityToken')
emclab commented 2 years ago

keysecret是阿里提供的,不能为空。