器上的“ReferenceError: FormData is not defined”"/>
Node.js Express 服务器上的“ReferenceError: FormData is not defined”
我使用 Node.js 和 TypeScript 编写了一个快速服务器。
服务器可以接收一组图像,并应使用请求正文中带有表单数据实例的 HTTP POST 请求将它们进一步发送到外部 API。
在本地运行时它运行良好,但在部署到渲染托管服务后,我从“Try and Catch”的 Catch 部分收到此错误:
Apr 24 05:57:33 PM Something went wrong, ReferenceError: FormData is not defined
我尝试安装并导入 npm 的表单数据包,但在将 formData 实例分配到请求的 bodt 时出现不同的错误:
Type 'FormData' is not assignable to type 'BodyInit'.
Type 'FormData' is missing the following properties from type 'URLSearchParams': delete, get, getAll, has, and 7 more.ts(2322)
(property) RequestInit.body?: BodyInit
A BodyInit object or null to set request's body.
有人知道这是为什么吗?
这是运行 HTTP POST 请求的代码:
export class PlanetNetDao extends AbstractDao{
readonly basicURL: string
#API_KEY: string
constructor(){
super()
this.basicURL = ''
this.#API_KEY = process.env.PLANET_NET_API_KEY
}
async fetchIdentifyPlantPost(originalImageNames: string[]) {
try {
const formData = new FormData()
for (let i = 0 ; i < originalImageNames.length ; i++) {
const imageFile = this.fsReadFileSync(originalImageNames[i])
const blob = new Blob([imageFile], { type: 'image/jpeg' })
formData.append('images', blob)
this.removeImageFromStorage(originalImageNames[i])
}
const response = await fetch (`${this.basicURL}/v2/identify/all?include-related-images=true&no-reject=false&lang=en&api-key=${this.#API_KEY}`,
{method: 'POST',
body: formData})
const res = await response.json()
return res
} catch (err) {
console.log('Something went wrong, ' + err)
throw err
}
}
}
回答如下:
更多推荐
Node.js Express 服务器上的“ReferenceError: FormData is not defined”
发布评论