express multer无法读取未定义的属性“ profileimage”

奥尔利卡·头脑风暴

我正在尝试提交一个表单,其中包含文件输入,名称为profileimage,我正在使用Express 4,Express Generator和Multer。

在我中,app.js我添加了multer,如下所示:

var multer = require('multer');
var upload = multer({ dest: './uploads' });

我遵循的教程实际上是这样设置multer的(在需要它之后),但是它给了我一个错误:

app.use(multer({dest: './uploads'}));

routes相应文件内的文件夹中,我具有以下内容:

router.post('/register', function(req, res, next) {
// get form values
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;

// check for image field
if (req.files.profileimage)
{
    console.log('Uploading file...');
...

但是,当我提交表单时,出现以下错误:

Cannot read property 'profileimage' of undefined

似乎无法理解req.files.profileimage,但我不知道为什么?

mscdex

multer前不久,该API进行了更改,因此那里的一些教程仍然使用旧的API。

对于单个文件,您将执行以下操作:

var uploads = multer({dest: './uploads'});

// ...

router.post('/register', uploads.single('profileimg'), function(req, res, next) {

   // ...

   if (req.file) {
     console.log('Profile image uploaded');
   }
});

请参阅multer文档以获取其他方案的更多示例。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:无法读取未定义的React Express的属性'prototype'

Express.js无法读取未定义的属性“ req”

无法读取未定义的属性“ destroy”-Express JS Session

Express Sequelize错误-无法读取未定义的属性'findAll'

nodeJS/Express TypeError:无法读取未定义的属性“方法”

Express:无法读取未定义的属性“forEach”

Express JS:无法读取未定义的属性“ lazyrouter”

无法读取未定义的Express API的属性“ count”

TypeError:无法读取未定义Express的属性“使用”

express :: server :: routerTypeError:无法读取未定义的属性“ length”

节点/ Express JS“无法读取未定义的属性'用户名'”

类型错误:无法读取 Express 上未定义的属性“get”

axios TypeError:无法读取 express 中未定义的属性“协议”

TypeError:无法读取未定义的Node.js Express MVC的属性'Render'

无法在Express 4的错误处理程序中读取未定义的属性'domain'

express-openapi-validator:错误:TypeError:无法读取未定义的属性“模式”

Express.js + Mongo:TypeError:无法读取未定义的属性

NodeJS / Express为什么会出现“无法读取未定义的属性'then'”的信息?

类型错误:无法读取 express、postgres、sequelize 中未定义错误的属性“create”

尝试上传图像时无法读取 express 中未定义的属性“文件”

无法读取未定义的multer的属性

Express.js 应用程序错误:无法读取未定义的属性(读取 'transfer-encoding'

类型错误:无法读取 node.JS 中未定义的属性“用户名”/express 无法发布到路由

Vue 前端 - Multer Express - Req.File 未定义

Multer - 无法读取未定义的属性“路径”

声明路由时出现一般的NodeJs / express错误(TypeError:无法读取pathtoRegexp上未定义的属性“ length”)

Sequelize.js / Node.js / Express.js:Tasks.findAll()返回TypeError:无法读取未定义的属性'findAll'

Express.js-无法在会话变量中设置未定义的属性“ userId”

nodeJS + Express-Session 错误:无法设置未定义的属性“登录”