Node.js Express 服务器上的“ReferenceError: FormData is not defined”

编程入门 行业动态 更新时间:2024-10-06 14:25:20

Node.js Express 服务<a href=https://www.elefans.com/category/jswz/34/1771452.html style=器上的“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”

本文发布于:2024-05-30 10:20:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器上   Express   js   Node   FormData

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!