You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Dropbox JavaScript SDK的filesUpload()时遇RequestInit错误

问题描述

我在React写歌应用中集成Dropbox API,已完成用户验证、获取Auth Token并实现页面跳转,但无法将文件上传至Dropbox,也无法把链接保存到数据库。问题出在服务器端这段代码:

const dropboxResponse = await dbx.filesUpload({
    path: `/uploads/songs/${uploadedSong.originalname}`,
    contents: songFileStream
});

提交上传时收到错误:

Error submitting new song in Database TypeError: RequestInit: duplex option is required when sending a body.
    at Object.fetch (node:internal/deps/undici/undici:11457:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///Users/leedyer/Desktop/Coding/WABS/Server/server.js:232:27

我曾尝试在filesUpload的contents中添加"duplex: true",但没有解决问题。

解决方案
  • 升级Dropbox JavaScript SDK:这个错误是Node.js 18+的fetch API要求发送带body的请求时必须指定duplex选项,旧版本Dropbox SDK未适配该变化。执行npm install dropbox@latest升级到最新版,新版本已修复此兼容问题。
  • 手动配置fetch选项:如果升级SDK后问题仍存在,初始化Dropbox客户端时手动传入带有duplex参数的fetch配置:
    const { Dropbox } = require('dropbox');
    const dbx = new Dropbox({
        accessToken: 'YOUR_AUTH_TOKEN',
        fetch: (url, options) => {
            options.duplex = 'half';
            return fetch(url, options);
        }
    });
    
  • 检查文件流状态:确保songFileStream是有效的可读流,且没有在上传前被提前读取或消耗。可以通过重新创建流(如从req.file重新获取)来验证。

内容的提问来源于stack exchange,提问作者LeeDyer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 20:12:07