【问题标题】:Multer | keep getting "The "path" argument must be of type string. Received type number "穆尔特 |不断收到““路径”参数必须是字符串类型。收到的类型号“
【发布时间】:2021-06-16 23:03:53
【问题描述】:

我试图弄清楚如何使用 Multer 将视频从磁盘存储上传到 MongoDB,但我不断收到““路径”参数必须是字符串类型。接收到的类型号”。

如果有人可以帮助指导我正确的方向,那将不胜感激!

存储和存储上传

const uploadStorage = multer.diskStorage({
    destination: (req, file, cb)=> {
        cb(null, './uploads/')
    },
    filename: (req, file, cb)=> {
        cb(null, Date.now(), + ' - ' + file.originalname)
    }
})

const storageUpload = multer({
    storage: uploadStorage
})

发布路线

app.post('/upload', storageUpload.single('videoUpload'), (req, res) => {
    const newVideoUpload = new uploadMongo({
        video: {
            data: fs.readFileSync(path.join(__dirname + '/uploads/' + req.file.filename)),
            contentType: 'video/mp4'
        }
    })

    newVideoUpload.save((err, data)=> {
        if(err) {
            throw err
        } else {
            res.redirect('/')
        }
    })
})

MongoDB 架构

const mongoose = require('mongoose')

const uploadSchema = mongoose.Schema({
    video: {
        data: Buffer,
        contentType: String
    }
}, {timestamps: true})

const uploadModel = mongoose.model('video', uploadSchema)
module.exports = uploadModel;

html 页面

<html>
    <head>
        <title><%= title %></title>
    </head>
    <body>
        <h3>Upload a video</h3>
        <form action="/upload" method="POST" enctype="multipart/form-data">
            <input type="file" name="videoUpload">
            <button type="submit">Upload</button>
        </form>
    </body>
</html>

错误

node:internal/validators:129
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type number (1623854458960)
    at new NodeError (node:internal/errors:329:5)
    at validateString (node:internal/validators:129:11)
    at Object.join (node:path:397:7)
    at C:\Users\gabri\Desktop\website\node_modules\multer\storage\disk.js:37:28
    at DiskStorage.filename [as getFilename] (C:\Users\gabri\Desktop\website\server.js:26:9)
    at C:\Users\gabri\Desktop\website\node_modules\multer\storage\disk.js:34:10
    at DiskStorage.destination [as getDestination] (C:\Users\gabri\Desktop\website\server.js:23:9)
    at DiskStorage._handleFile (C:\Users\gabri\Desktop\website\node_modules\multer\storage\disk.js:31:8)
    at C:\Users\gabri\Desktop\website\node_modules\multer\lib\make-middleware.js:144:17
    at allowAll (C:\Users\gabri\Desktop\website\node_modules\multer\index.js:8:3)
    at wrappedFileFilter (C:\Users\gabri\Desktop\website\node_modules\multer\index.js:44:7)
    at Busboy.<anonymous> (C:\Users\gabri\Desktop\website\node_modules\multer\lib\make-middleware.js:114:7)       
    at Busboy.emit (node:events:369:20)
    at Busboy.emit (C:\Users\gabri\Desktop\website\node_modules\busboy\lib\main.js:38:33)
    at PartStream.<anonymous> (C:\Users\gabri\Desktop\website\node_modules\busboy\lib\types\multipart.js:213:13)
    at PartStream.emit (node:events:369:20) {
  code: 'ERR_INVALID_ARG_TYPE'
}

【问题讨论】:

    标签: node.js mongodb file mongoose multer


    【解决方案1】:

    错误来自这里:

    cb(null, Date.now(), + ' - ' + file.originalname)

    额外的, 导致Date.now() 作为不带字符串运算符的文件名传递。 Date.now() 返回一个数字。

    试试:

    cb(null, String(Date.now()), + ' - ' + file.originalname)
    

    或者

    cb(null, Date.now() + ' - ' + file.originalname)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-20
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2021-09-08
      • 2020-12-30
      • 1970-01-01
      相关资源
      最近更新 更多