resolveLocalFileSystemURL TypeError:无法读取未定义TypeError的属性“ then”

时间:2020-02-17 21:53:15

标签: ionic-framework capacitor

我正在研究离子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); });

请注意,此函数返回一个诺言

3 个答案:

答案 0 :(得分:0)

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

paramName

修改

您确定该方法返回承诺吗?根据一些文档,该方法接受callbakc函数。因此,您的代码将类似于:

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

...

constructor(private _file: File) { }

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

编辑2

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

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

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

Official Ionic Documentation Official plugin documentation

答案 1 :(得分:0)

我遇到了同样的问题,但是找到了解决方案! 您已经安装了离子本机文件插件吗? 我不知道电容器,但与科尔多瓦: 离子cordova插件添加cordova-plugin-文件

答案 2 :(得分:0)

如果收到此错误消息:

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

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

您是否已将所需的权限添加到config.xml?

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