dcloudio / uni-app

A cross-platform framework using Vue.js
https://uniapp.dcloud.io
Apache License 2.0
39.69k stars 3.61k forks source link

云函数中使用官方文档案例发送formdata对象,使用云端云函数请求时报错.getBuffer is not a function #3540

Closed LTorch closed 2 years ago

LTorch commented 2 years ago

问题描述 根据 https://uniapp.dcloud.io/uniCloud/cf-functions.html#httpclient 中提供的案例 image 使用该方法在本地云函数调试后可以正常发送请求,但是在部署unicloud云函数后,调用该函数却报错form.getBuffer is not a function

注:贴出的demo代码用于复现bug,我开发调用的接口地址是微信api的,请求需要证书和签名,所以这里放出的是我复现出bug的demo。 云空间服务商为腾讯云。 复现步骤 [复现问题的步骤]

'use strict';
const FormData = require('form-data'); // 此form-data需要使用npm安装,地址:https://www.npmjs.com/package/form-data
exports.main = async (event, context) => {
  const form = new FormData()
  form.append('otherParam', 'otherParam content');
  const res = await uniCloud.httpclient.request('https://httpbin.org/post', {
    method: 'POST',
    content: form.getBuffer(), // 请求内容
    headers: form.getHeaders(), // 请求头
    dataType: 'json' // 此处指定为json表示将此请求的返回值解析为json
  })
  return res
};

预期结果 本地调试的时候上传成功 image

实际结果 部署到unicloud云函数后请求报错 form.getBuffer is not a function image

系统信息:

Wangyaqi commented 2 years ago

阿里云还是腾讯云?

LTorch commented 2 years ago

腾讯云

Wangyaqi commented 2 years ago

我这试了一下云端运行正常,你用的form-data版本是多少?nodejs版本多少?

LTorch commented 2 years ago

form-data版本"4.0.0",node版本8.9,我再去试一下看看,谢谢帮助:D

LTorch commented 2 years ago

image 还是一样,本地云函数可用,上传到云端后就不行了,第一条控制台是本地调试,第二条是请求云函数

Wangyaqi commented 2 years ago

你的云函数内容不是上面发的那个示例吧,报错行号对不上。你把你的代码发出来看看

LTorch commented 2 years ago

是这个,报错行是前端报错的log行数不一样。 image image

Wangyaqi commented 2 years ago

云端打印一下require('form-data/package.json')看一下和你安装的是不是一致

LTorch commented 2 years ago
{
    "result": {
        "author": "Felix Geisendörfer <felix@debuggable.com> (http://debuggable.com/)",
        "name": "form-data",
        "description": "A library to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.",
        "version": "4.0.0",
        "repository": {
            "type": "git",
            "url": "git://github.com/form-data/form-data.git"
        },
        "main": "./lib/form_data",
        "browser": "./lib/browser",
        "typings": "./index.d.ts",
        "scripts": {
            "pretest": "rimraf coverage test/tmp",
            "test": "istanbul cover test/run.js",
            "posttest": "istanbul report lcov text",
            "lint": "eslint lib/*.js test/*.js test/integration/*.js",
            "report": "istanbul report lcov text",
            "ci-lint": "is-node-modern 8 && npm run lint || is-node-not-modern 8",
            "ci-test": "npm run test && npm run browser && npm run report",
            "predebug": "rimraf coverage test/tmp",
            "debug": "verbose=1 ./test/run.js",
            "browser": "browserify -t browserify-istanbul test/run-browser.js | obake --coverage",
            "check": "istanbul check-coverage coverage/coverage*.json",
            "files": "pkgfiles --sort=name",
            "get-version": "node -e \"console.log(require('./package.json').version)\"",
            "update-readme": "sed -i.bak 's/\\/master\\.svg/\\/v'$(npm --silent run get-version)'.svg/g' README.md",
            "restore-readme": "mv README.md.bak README.md",
            "prepublish": "in-publish && npm run update-readme || not-in-publish",
            "postpublish": "npm run restore-readme"
        },
        "pre-commit": [
            "lint",
            "ci-test",
            "check"
        ],
        "engines": {
            "node": ">= 6"
        },
        "dependencies": {
            "asynckit": "^0.4.0",
            "combined-stream": "^1.0.8",
            "mime-types": "^2.1.12"
        },
        "devDependencies": {
            "@types/node": "^12.0.10",
            "browserify": "^13.1.1",
            "browserify-istanbul": "^2.0.0",
            "coveralls": "^3.0.4",
            "cross-spawn": "^6.0.5",
            "eslint": "^6.0.1",
            "fake": "^0.2.2",
            "far": "^0.0.7",
            "formidable": "^1.0.17",
            "in-publish": "^2.0.0",
            "is-node-modern": "^1.0.0",
            "istanbul": "^0.4.5",
            "obake": "^0.1.2",
            "puppeteer": "^1.19.0",
            "pkgfiles": "^2.3.0",
            "pre-commit": "^1.1.3",
            "request": "^2.88.0",
            "rimraf": "^2.7.1",
            "tape": "^4.6.2",
            "typescript": "^3.5.2"
        },
        "license": "MIT"
    }
}
Wangyaqi commented 2 years ago

换个云函数名,使用示例里面的代码运行一下试试呢

LTorch commented 2 years ago

image 换了,还是一样XD

LTorch commented 2 years ago

node换了个版本就可以了,可能是环境问题吧XD image

1064094732 commented 3 weeks ago

切换版本也还是这样的情况

1064094732 commented 3 weeks ago

不放在公共模块里面就可以

1064094732 commented 3 weeks ago

不放在公共模块里面就可以