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

Node.js中Firebase图片上传报错TypeError [ERR_INVALID_ARG_VALUE]求助

问题:Node.js上传图片到Firebase时触发TypeError错误

代码实现

// Controller function for uploading pictures
const uploadPicture = async (req, res) => {
  try {
    const { xId } = req.params;
    const file = req.file;

    if (!file) {
      return res.status(400).send("No file uploaded.");
    }

    // Define the bucket file name
    const bucketFileName = `x/${xId}/${file.originalname}`;

    await bucket.upload(file.buffer, {
      gzip: true,
      destination: bucketFileName, // Use the defined bucketFileName
      metadata: {
        cacheControl: "public, max-age=31536000",
        contentType: file.mimetype,
      },
    });

    const pictureData = {
      x: xId,
      path: `gs://${bucket.name}/${bucketFileName}`,
    };

    // Save the picture data to the Firestore database
    await db
      .collection("users")
      .doc("x")
      .update({
        comments: admin.firestore.FieldValue.arrayUnion(pictureData),
      });

    res.status(200).send("Picture uploaded successfully.");
  } catch (error) {
    console.error("Error:", error);
    res.status(500).send("Internal Server Error");
  }
};

触发的错误信息

Error: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer ...>

获取到的file对象信息

{
  fieldname: 'file',
  originalname: 'example-preview(11).png',
  encoding: '7bit',
  mimetype: 'image/png',
  buffer: <Buffer 89 50 4e 47 ... 10133 more bytes>,
  size: 10133
}

错误原因分析

bucket.upload()方法的设计逻辑是接收本地文件的路径字符串,用于上传磁盘中的文件。但你当前传入的是file.buffer(Buffer对象),与方法预期的参数类型不匹配,因此触发了参数无效的TypeError。

解决建议

由于你使用内存存储(multer的memoryStorage),文件内容以Buffer形式存在,需改用bucket.file().save()方法直接写入Buffer数据,替换原有的bucket.upload()调用。

修改后的核心代码片段

// 替换原来的bucket.upload部分
const fileRef = bucket.file(bucketFileName);
await fileRef.save(file.buffer, {
  gzip: true,
  metadata: {
    cacheControl: "public, max-age=31536000",
    contentType: file.mimetype,
  },
});

完整修改后的控制器函数

const uploadPicture = async (req, res) => {
  try {
    const { xId } = req.params;
    const file = req.file;

    if (!file) {
      return res.status(400).send("No file uploaded.");
    }

    const bucketFileName = `x/${xId}/${file.originalname}`;
    // 使用save方法上传Buffer
    const fileRef = bucket.file(bucketFileName);
    await fileRef.save(file.buffer, {
      gzip: true,
      metadata: {
        cacheControl: "public, max-age=31536000",
        contentType: file.mimetype,
      },
    });

    const pictureData = {
      x: xId,
      path: `gs://${bucket.name}/${bucketFileName}`,
    };

    await db
      .collection("users")
      .doc("x")
      .update({
        comments: admin.firestore.FieldValue.arrayUnion(pictureData),
      });

    res.status(200).send("Picture uploaded successfully.");
  } catch (error) {
    console.error("Error:", error);
    res.status(500).send("Internal Server Error");
  }
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 03:17:15