【问题标题】:Image Upload Error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined图片上传错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。接收类型未定义
【发布时间】:2021-01-22 19:36:01
【问题描述】:

我正在尝试使用邮递员将图像上传到 Firebase。随着firebase serve 的运行,我向我的路由发送了一个发布请求以及授权标头和图像文件,但在控制台中收到以下错误:

TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。接收类型未定义

exports.uploadImage = (req, res) => {
  const BusBoy = require('busboy')
  const path = require('path')
  const os = require('os')
  const fs = require('fs')

  const busboy = new BusBoy({
    headers: req.headers
  })

  let imageFileName
  let imageToBeUploaded = {}

  busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {

    console.log(fieldname, filename, encoding, mimetype)

    if (mimetype !== 'image/jpeg' && mimetype !== 'image/png') {
      return res.status(400).json({
        error: '❌ Wrong file type submitted'
      })
    }

    const imageExtension = filename.split('.')[filename.split('.').length - 1]

    imageFileName = `${Math.round(
      Math.random() * 1000000000000
    )}.${imageExtension}`

    const filepath = path.join(os.tmpdir(), imageFileName)

    imageToBeUploaded = {
      filepath,
      mimetype
    }

    file.pipe(fs.createWriteStream(filepath))

  })


  busboy.on('finish', () => {
    admin
      .storage()
      .bucket(config.storageBucket)
      .upload(imageToBeUploaded.filpath, {
        resumable: false,
        metadata: {
          metadata: {
            contentType: imageToBeUploaded.mimetype,
          },
        },
      })
      .then(() => {
        const imageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`
        return db.doc(`/users/${req.user.handle}`).update({
          imageUrl
        })
      })
      .then(() => {
        return res.json({
          message: '✅ Image uploaded successfully'
        })
      })
      .catch((err) => {
        console.error(err)
        return res.status(500).json({
          error: err.code
        })
      })
  })
  busboy.end(req.rawBody)
}

完整的错误信息:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
     at validateString (internal/validators.js:112:11)
     at Object.basename (path.js:1157:5)
     at Bucket.upload (/Users/apple/Code/litter/litter-functions/functions/node_modules/@google-cloud/storage/build/src/bucket.js:2493:38)
     at /Users/apple/Code/litter/litter-functions/functions/node_modules/@google-cloud/promisify/build/src/index.js:69:28
     at new Promise (<anonymous>)
     at Bucket.wrapper (/Users/apple/Code/litter/litter-functions/functions/node_modules/@google-cloud/promisify/build/src/index.js:54:16)
     at Busboy.<anonymous> (/Users/apple/Code/litter/litter-functions/functions/handlers/users.js:133:8)
     at Busboy.emit (events.js:210:5)
     at Busboy.emit (/Users/apple/Code/litter/litter-functions/functions/node_modules/busboy/lib/main.js:37:33)
     at /Users/apple/Code/litter/litter-functions/functions/node_modules/busboy/lib/types/multipart.js:52:13 {    code: 'ERR_INVALID_ARG_TYPE'  }

【问题讨论】:

  • 请编辑问题以显示完整的错误,并确保指出是哪一行代码导致了它。您在需要字符串的地方传递 undefined。

标签: javascript node.js firebase firebase-storage busboy


【解决方案1】:

问题是您在upload() 的参数中输入了imageToBeUploaded.filpath,但您的本意是输入imageToBeUploaded.filepath。缺少一个“e”,这使得整个表达式未定义。

【讨论】:

  • 谢谢!修复了它:)
猜你喜欢
  • 2020-09-21
  • 2022-09-29
  • 2019-12-14
  • 2021-09-08
  • 2020-12-30
  • 2020-07-13
  • 2022-01-12
  • 2021-12-12
相关资源
最近更新 更多