离子上传错误:高级-http:“params”选项需要是字典样式对象,<params:{[key:string]:string | 字符串[]}>

Lou*_*nda 4 javascript cordova ionic-framework cordova-plugins cordova-plugin-advanced-http

使用以下方法将相机/图像文件从设备上传到 API 时,我只想使用离子插件https://github.com/silkimen/cordova-plugin-advanced-http#uploadFile 。

\n\n

然而,根据下面评论的文档,它没有任何语法错误。

\n\n

这个问题与参数有关,我不明白为什么......

\n\n
\n

错误:高级-http:“params”选项需要是字典样式\n对象,

\n
\n\n

在此输入图像描述

\n\n

上传图片到api的方法。

\n\n
async uploadToAPI(imgEntry) {\n\n    var url = environment.api_host + "/activities/log/images"\n    var filePath = imgEntry.localURL;\n    var name = \'reports\';\n\n    var body = {\n        user_hash: \'xxxxxxx\',\n        activities_id: 1,\n        activities_log_id: 1\n    }\n\n    var headers = {\n        \'Authorization\': \'Bearer \' + environment.api_security_bearer\n    }\n\n    // (method) HTTP.uploadFile(url: string, body: any, headers: any, filePath: string | string[], name: string | string[]): Promise<any>\n    // @param url \xe2\x80\x94 The url to send the request to\n    // @param body \xe2\x80\x94 The body of the request\n    // @param headers \xe2\x80\x94 The headers to set for this request\n    // @param filePath \xe2\x80\x94 The local path(s) of the file(s) to upload\n    // @param name \xe2\x80\x94 The name(s) of the parameter to pass the file(s) along as\n    // @returns \xe2\x80\x94 returns a FileEntry promise that will resolve on success, and reject on failure\n\n    this.http.uploadFile(\n        url,\n        body,\n        headers,\n        filePath,\n        name,\n    ).then((data) => {\n        console.log(data)\n    })\n        .catch(error => {\n            console.log(error)\n        })\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

上述以下代码中的错误可能会导致我遗漏什么?

\n\n

PS:只是,想使用 ionic-native/http/ngx 而没有其他。

\n\n
import { HTTP } from \'@ionic-native/http/ngx\';\n
Run Code Online (Sandbox Code Playgroud)\n

小智 5

我有类似上面的错误,OP帖子,它指出“param”得到错误键:string..

经过进一步调试,离子电容器运行时仅检查 value = String | 仅数组。我意识到我的身体蕴藏着长期的价值。因此,将值更改为字符串,一切正常。

只需在正文中为非字符串值添加 String() 即可。如果仍然错误,则同样适用于标题。

var body = {
        user_hash: 'xxxxxxx',
        activities_id: String(1),
        activities_log_id: String(1)
    }
Run Code Online (Sandbox Code Playgroud)

  • 我有一个参数是整数,将其转换为字符串解决了问题。谢谢。 (2认同)