【问题标题】:resolveLocalFileSystemURL TypeError: Cannot read property 'then' of undefined TypeErrorresolveLocalFileSystemURL TypeError:无法读取未定义 TypeError 的属性“then”
【发布时间】:2020-07-02 17:57:31
【问题描述】:

我正在研究离子 4(电容器)。我正在尝试解析本地文件系统 URL,所以我使用了这个函数 resolveLocalFilesystemUrl 但不幸的是,我收到了这个错误

TypeError: Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined

这是我的代码: constructor(private _file: NativeFile) {} ... .. this._file.resolveLocalFilesystemUrl(nativeFilePath).then((entry: Entry) => { console.log(entry); });

注意这个函数返回一个promise

【问题讨论】:

  • 根据文档,它应该返回一个承诺。尽管如此,如果我检查它,它会返回未定义,这正是引发错误的原因。对我来说,这发生在带有 Cordova 的 Ionic 5 上。所以,这涵盖了广泛的可能性,介于 4 和电容器之间。
  • 问题:您的错误是在桌面浏览器中运行还是部署到手机后出现?对我来说,它在浏览器中,尚未部署到 Android。
  • 只是这样您就不必尝试了:认为这可能与 File 需要在检查 github 页面上的 cordova 文件插件后准备好平台有关 [github.com/apache/cordova-plugin-file] 其中,反过来,下面提到的 ionicframework.com 上的 File API 的(基本)手册也引用了它。因此,我将使用 File 的例程包装到 platform.ready.then() 承诺中,这将我在单元测试中遇到的错误减少为警告,但仍然与错误具有相同的消息,除此之外,没有任何效果。

标签: ionic-framework capacitor


【解决方案1】:

确保您使用的是正确的插件。这是你需要的:https://ionicframework.com/docs/native/file

import { File } from '@ionic-native/file/ngx';

...

constructor(private _file: File) { }

this._file.resolveLocalFilesystemUrl(nativeFilePath).then((entry: Entry) => {
   console.log(entry);
});

编辑

您确定该方法返回一个承诺吗?根据一些文档,该方法接受一个 callbakc 函数。所以你的代码会是这样的:

this._file.resolveLocalFilesystemUrl(
  nativeFilePath,
  (entry: Entry) => console.log(entry),
  err => console.log(err)
);

编辑 2

我正在开发一个基于 cordova 的项目(Ionic 4),我正在使用官方建议的插件:https://ionicframework.com/docs/native/file。它也在电容器项目中得到支持。 形成官方文档:

您还需要一个 FileEntry 对象来读取现有文件。使用 FileEntry的file属性获取文件引用,然后创建 一个新的 FileReader 对象。您可以使用 readAsText 之类的方法来启动 读操作。当读取操作完成时,this.result 存储读取操作的结果。

function readFile(fileEntry) {
  fileEntry.file(function (file) {
    var reader = new FileReader();

    reader.onloadend = function() {
      console.log("Successful file read: " + this.result);
      displayFileData(fileEntry.fullPath + ": " + this.result);
    };

    reader.readAsText(file);

  }, onErrorReadFile);
}

Official Ionic Documentation Official plugin documentation

【讨论】:

  • 感谢您的回复。我已经更新了我正在使用的插件,但我仍然面临同样的问题:(
  • 我仍然面临同样的问题:(
  • 哦,哇,那我只是参加聚会。这里同样的问题。文档说它应该返回一个 Promise ionicframework.com/docs/v3/native/file 当我尝试使用回调函数的方法时,我被 Sublime 标记为“预期 1 个参数但得到 3 个”。
  • 另外,@Gabriel Bianconi:您能否参考您上面提到的“一些文档”?
  • 如果我使用 readAsText 而不是 resolveLocalFilesystemUrl,我会收到类似的错误。嗯,实际上,错误信息是完全相同相同
【解决方案2】:

我遇到了同样的问题,但我找到了解决方案! 你安装了离子原生文件插件吗? 我不知道电容器,但有科尔多瓦: 离子cordova插件添加cordova-plugin-file

【讨论】:

    【解决方案3】:

    如果您收到此错误消息:

    TypeError: Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined
    

    插件可能没有权限访问存储,导致所有函数和属性返回未定义。

    您是否在 config.xml 中添加了所需的权限?

    <preference name="AndroidPersistentFileLocation" value="Compatibility" />
    

    【讨论】:

      猜你喜欢
      • 2019-08-19
      • 2014-09-07
      • 1970-01-01
      • 2014-11-26
      • 2014-11-29
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多