Search code examples
node.jsmongodbfilemongoosemulter

Multer | keep getting "The "path" argument must be of type string. Received type number "


I'm trying to figure out how to upload videos from disk storage onto MongoDB using Multer but I keep getting "The "path" argument must be of type string. Received type number".

If somebody could help guide me onto the right direction, that would be greatly appreciated!

Storage and Storage Upload

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
})

Post route

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 Schema

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 page

<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>

Bug

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'
}

Solution

  • The error is coming from here:

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

    The extra , is causing the Date.now() to be passed as the filename without the string operators. Date.now() returns a number.

    Try:

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

    Or

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