穆尔特 |继续获取 "The "path" 参数必须是字符串类型。接收到的类型编号 "
Multer | keep getting "The "path" argument must be of type string. Received type number "
我正在尝试弄清楚如何使用 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
})
Post路线
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'
}
错误来自这里:
cb(null, Date.now(), + ' - ' + file.originalname)
额外的 ,
导致 Date.now()
在没有字符串运算符的情况下作为文件名传递。 Date.now()
returns 一个数字。
尝试:
cb(null, String(Date.now()), + ' - ' + file.originalname)
或
cb(null, Date.now() + ' - ' + file.originalname)
我正在尝试弄清楚如何使用 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
})
Post路线
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'
}
错误来自这里:
cb(null, Date.now(), + ' - ' + file.originalname)
额外的 ,
导致 Date.now()
在没有字符串运算符的情况下作为文件名传递。 Date.now()
returns 一个数字。
尝试:
cb(null, String(Date.now()), + ' - ' + file.originalname)
或
cb(null, Date.now() + ' - ' + file.originalname)