简体   繁体   English

React Native [TypeError:无法读取未定义的属性'startsWith']?

[英]React Native [TypeError: Cannot read property 'startsWith' of undefined]?

I'm getting this error when I put my Javascipt code to a Typescript project.将 Javascipt 代码放入 Typescript 项目时出现此错误。 In Javascript project its worked fine but when I put the code into a Typescript project I got the error.在 Javascript 项目中,它运行良好,但是当我将代码放入 Typescript 项目时,我得到了错误。

Is there any mistake in coding?编码有错误吗?

Here's the code :这是代码:

const createPDF = async() => {
    let Options = {
      html: '<h1>test</h1>',
      fileName: 'test',
      directory: 'Download',
    };

    let file = await RNHTMLtoPDF.convert(Options)
    Alert.alert('Alert','Successfully exported file:' + file.filePath,[
      {text:'Cancel',style:'cancel'},
      {text:'Open',onPress:()=>openFile(file.filepath)}
    ],{cancelable:true});
  }

  const openFile = (filepath: any) => {
    const path = filepath;
    FileViewer.open(path)
    .then(()=>{
      //success
      console.log(`Successfully export PDF file`);
      
    })
    .catch(error => {
      console.log(error);
    });
  }
LOG      [TypeError: Cannot read property 'startsWith' of undefined]

finally I have solved the problem by adding string type and :void to filepath instead of any ( i dont know what this mean but it worked )最后我通过添加string类型和:void文件路径而不是any解决了这个问题(我不知道这是什么意思,但它有效)

  const openFile = (filepath: string):void => {
    const path = filepath;
    FileViewer.open(path, { showOpenWithDialog: true })
    .then(()=>{
      //success
      console.log(`Successfully export PDF file`);
      
    })
    .catch(error => {
      console.log(error);
    });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript TypeError:无法读取未定义的属性&#39;startsWith&#39;-Discord Bot - JavaScript TypeError: Cannot read property 'startsWith' of undefined - discord bot 类型错误:无法读取新 DataSnapshot 中未定义的属性“startsWith” - TypeError: Cannot read property 'startsWith' of undefined at new DataSnapshot JavaScript(discord.js)TypeError:无法读取未定义的属性“startsWith” - JavaScript(discord.js) TypeError: Cannot read property 'startsWith' of undefined Discord.JS UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“startsWith” - Discord.JS UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined 无法读取未定义的属性“startsWith” - Cannot read property 'startsWith' of undefined 类型错误:无法读取 React Native 中未定义的属性“set” - TypeError: Cannot read property 'set' of undefined in React native "React-Native AsyncStorage:TypeError:无法读取未定义的属性“setItem”" - React-Native AsyncStorage: TypeError: Cannot read property 'setItem' of undefined React Native TypeError:无法读取未定义的属性'projectName' - React Native TypeError: Cannot read property 'projectName' of undefined react-native - 类型错误:无法读取未定义的属性“修剪” - react-native - TypeError: Cannot read property 'trim' of undefined React Native jest test:TypeError:无法读取未定义的属性&#39;unsubscribeFromTopic&#39; - React Native jest test: TypeError: Cannot read property 'unsubscribeFromTopic' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM