app.post('/upload/image', multer({
dest: './public/users/',
changeDest: function(dest, req, res) {
var newDestination = dest ;
var stat = null;
try {
stat = fs.statSync(newDestination);
} catch (err) {
fs.mkdirSync(newDestination);
}
if (stat && !stat.isDirectory()) {
throw new Error("directory cant be created");
}
return newDestination
}
}), function(req, res) {
res.send({"status":"success"})
});
我尝试使用此代码,以便可以动态设置multer的路径以存储来自客户端的上载图片。但是这段代码给了我这个错误
throw new Error(msg);
^
Error: Route.post() requires a callback function but got a [object Object]
at Route.<computed> [as post] (/Volumes/MacExtnd/Programming/webshop/backend/node_modules/express/lib/router/route.js:202:15)
at Function.app.<computed> [as post] (/Volumes/MacExtnd/Programming/webshop/backend/node_modules/express/lib/application.js:482:19)
at Object.<anonymous> (/Volumes/MacExtnd/Programming/webshop/backend/server.js:733:5)
请任何人告诉我如何解决此错误?
答案 0 :(得分:0)
multer(opts)
返回具有.single()
,.none()
之类的属性的对象。 See docs here。
您正在将此对象作为Express中间件函数传递,该函数解释了该错误。您可能想要选择正确的属性,然后传递该属性。
文档示例:
var upload = multer({ dest: 'uploads/' })
var app = express()
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})