'void |类型中不存在属性'data' FileUploadResult”

时间:2022-09-06 16:36:36

I developed a provider to manage upload/download of pictures in my Ionic3 app. This works well but now I would like to get the data returned by the promise that uploaded the file to the server. I could see the content I expected if I use the alert function but I could not access to the JSON content as my text editor reports me this error:

我开发了一个提供程序来管理我的Ionic3应用程序中的图片上传/下载。这很好但现在我想获取将文件上传到服务器的promise返回的数据。如果我使用alert函数,我可以看到我期望的内容,但是我无法访问JSON内容,因为我的文本编辑器报告了我这个错误:

Property 'data' does not exist on type 'void | FileUploadResult'

'void |类型中不存在属性'data' FileUploadResult”

Do you know what I am doing wrong?

你知道我做错了什么吗?

My code in my_page.ts:

my_page.ts中的代码:

uploadImage(newFileName) {
    this.attCtrl.uploadAttachment({ file: newFileName })
                .then((response) => {
                    alert(JSON.stringify(response)); // The JSON appears exactly like I expected
                    alert(response.data); // => Property 'data' does not exist on type 'void | FileUploadResult'
                })
                .catch((err)=> {
                    alert(JSON.stringify(err));
                })

    ;
}

then, my code in the provider:

那么,我在提供者中的代码:

public uploadAttachment(obj) {

    let targetPath = this.pathForImage(obj.file);
    let options: FileUploadOptions = {
        ...
    };

    const fileTransfer: FileTransferObject = this.transfer.create();

    return fileTransfer.upload(targetPath, url, options).then(data => {
        return data;
    }, err => {
        this.loading.dismissAll();
    });
}

1 个解决方案

#1


0  

Finally I managed to handle the response from FileUpload. First the interface FileUploadResult should be imported:

最后,我设法处理来自FileUpload的响应。首先应该导入FileUploadResult接口:

import { FileUploadResult } from '@ionic-native/file-transfer';

Then, set this interface FileUploadResult to the response, and parse the response:

然后,将此接口FileUploadResult设置为响应,并解析响应:

uploadImage(newFileName) {
    this.attCtrl.uploadAttachment({ file: newFileName, page: 'wo', target_id: this.wo.wo_id })
                .then((r: FileUploadResult) => {
                    let obj = JSON.parse(r.response);
                    // Then obj can be handled
                })
                .catch(err => {
                    alert(err);
                });
}

#1


0  

Finally I managed to handle the response from FileUpload. First the interface FileUploadResult should be imported:

最后,我设法处理来自FileUpload的响应。首先应该导入FileUploadResult接口:

import { FileUploadResult } from '@ionic-native/file-transfer';

Then, set this interface FileUploadResult to the response, and parse the response:

然后,将此接口FileUploadResult设置为响应,并解析响应:

uploadImage(newFileName) {
    this.attCtrl.uploadAttachment({ file: newFileName, page: 'wo', target_id: this.wo.wo_id })
                .then((r: FileUploadResult) => {
                    let obj = JSON.parse(r.response);
                    // Then obj can be handled
                })
                .catch(err => {
                    alert(err);
                });
}