Node.js TypeError: Cannot read property 'file' of undefined

qliq

I'm just learning node.js and have difficulty to make a simple file upload using express and multer.

Here is the form:

Upload Image

In my configure.js I have:

app.use(express.static(path.join(__dirname, 'public')));
app.use(multer({dest:'../public/upload/temp'}).single('file'));

And the image.js controller:

create: function(req, res) {
        var saveImage = function() {
            console.log(req.body);
            var possible = 'abcdefghijklmnopqrstuvwxyz0123456789',
                imgUrl = '';

            for(var i=0; i < 6; i+=1) {
                imgUrl += possible.charAt(Math.floor(Math.random() * possible.length));
            }

            var tempPath = req.files.file.path, //<line 55 error
                ext = path.extname(req.files.file.name).toLowerCase(),
                targetPath = path.resolve('./public/upload/' + imgUrl + ext);

            if (ext === '.png' || ext === '.jpg' || ext === '.jpeg' || ext === '.gif') {


                fs.rename(tempPath, targetPath, function(err) {
                    if (err) throw err;

                    res.redirect('/images/' + imgUrl);
                });
            } else {
                fs.unlink(tempPath, function () {
                    if (err) throw err;

                    res.json(500, {error: 'Only image files are allowed.'});
                });
            }
        };

        saveImage();
    },

However I get this error when I try to upload an image:

TypeError: Cannot read property 'file' of undefined
    at saveImage (/home/pc/node-dev/test-proj/controllers/image.js:55:37)
    at module.exports.create (/home/pc/node-dev/test-proj/controllers/image.js:76:9)
    at Layer.handle [as handle_request] (/home/pc/node-dev/test-proj/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/pc/node-dev/test-proj/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/home/pc/node-dev/test-proj/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/pc/node-dev/test-proj/node_modules/express/lib/router/layer.js:95:5)
    at /home/pc/node-dev/test-proj/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/home/pc/node-dev/test-proj/node_modules/express/lib/router/index.js:330:12)
    at next (/home/pc/node-dev/test-proj/node_modules/express/lib/router/index.js:271:10)
    at urlencodedParser (/home/pc/node-dev/test-proj/node_modules/body-parser/lib/types/urlencoded.js:95:37)

And when I log the req object, file is not there:

{ title: 'myimage', description: 'something' }

Actually the snippet is just a slightly modified version what I read in this book, which is using outdated express-3. So basically I just updated it with the multer part.

I'm wondering what is wrong here and how to fix it.

maowtm

You are using upload.single, which you should use req.file not req.files. To upload multiple files, use upload.array.

Notice that you don't need another .file after req.file. req.file is the uploaded file if you are using upload.single.

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

如有侵权,请联系debugcn@gmail.com 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

require.js uncaught typeerror: cannot read property '__module_path__; of undefined

来自分类Dev

Vuex Cannot read property of undefined

来自分类Dev

TypeError:对象不是Node.js函数

来自分类Dev

jQuery Uncaught TypeError: Cannot read property 'nodeType' of undefined

来自分类Dev

Node.js:TypeError:对象不是函数

来自分类Dev

TypeError: Cannot call method 'get' of undefined

来自分类Dev

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'public_id' of undefined

来自分类Dev

Issue in typescript with generics: Cannot read property 'prototype' of undefined

来自分类Dev

Cannot read first line of a file

来自分类Dev

Hovertool Bokeh "Cannot read property"

来自分类Dev

使用Express在node.js中要求jQuery-ui时出现“ TypeError:undefined不是函数”

来自分类Dev

Uncaught TypeError: Cannot call method 'getLocalName' of undefined

来自分类Dev

Cannot read property 'errors' of undefined in angular 2

来自分类Dev

Cannot read property '$options' of undefined making external js file for head options

来自分类Dev

React Hooks Problem: Unhandled Rejection (TypeError): Cannot read property 'signup' of undefined

来自分类Dev

TypeError:使用Node.js fs.read()的错误参数

来自分类Dev

Node.js反复进行流式read.read调用

来自分类Dev

TypeError:undefined不是Node JS函数

来自分类Dev

TypeError:undefined不是Node.js中的函数

来自分类Dev

TypeError:undefined不是Node.js中管道的函数

来自分类Dev

Node.js中奇怪的TypeError

来自分类Dev

node.js TypeError:undefined不是app.get(“ /”)上的函数

来自分类Dev

了解控制台错误“TypeError: undefined is not an object”和“Uncaught TypeError: Cannot read property '0' of undefined”

来自分类Dev

typeerror undefined 不是 node js 中的函数

来自分类Dev

如何解决 Uncaught TypeError: Cannot read property 'call' of undefined?

来自分类Dev

显示“Uncaught TypeError: Can not read property 'length' of undefined”错误的原因是在 JavaScript 中

来自分类Dev

为什么我会收到 TypeError: Cannot read property 'length' of undefined?

来自分类Dev

在类构造函数中 this.button = document.getElementById('button') / Uncaught TypeError: Cannot read property 'addEventListener' of undefined?

来自分类Dev

如何克服 Node.js 中的“TypeError: Class extends value undefined is not a constructor or null”错误?

Related 相关文章

  1. 1

    require.js uncaught typeerror: cannot read property '__module_path__; of undefined

  2. 2

    Vuex Cannot read property of undefined

  3. 3

    TypeError:对象不是Node.js函数

  4. 4

    jQuery Uncaught TypeError: Cannot read property 'nodeType' of undefined

  5. 5

    Node.js:TypeError:对象不是函数

  6. 6

    TypeError: Cannot call method 'get' of undefined

  7. 7

    UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'public_id' of undefined

  8. 8

    Issue in typescript with generics: Cannot read property 'prototype' of undefined

  9. 9

    Cannot read first line of a file

  10. 10

    Hovertool Bokeh "Cannot read property"

  11. 11

    使用Express在node.js中要求jQuery-ui时出现“ TypeError:undefined不是函数”

  12. 12

    Uncaught TypeError: Cannot call method 'getLocalName' of undefined

  13. 13

    Cannot read property 'errors' of undefined in angular 2

  14. 14

    Cannot read property '$options' of undefined making external js file for head options

  15. 15

    React Hooks Problem: Unhandled Rejection (TypeError): Cannot read property 'signup' of undefined

  16. 16

    TypeError:使用Node.js fs.read()的错误参数

  17. 17

    Node.js反复进行流式read.read调用

  18. 18

    TypeError:undefined不是Node JS函数

  19. 19

    TypeError:undefined不是Node.js中的函数

  20. 20

    TypeError:undefined不是Node.js中管道的函数

  21. 21

    Node.js中奇怪的TypeError

  22. 22

    node.js TypeError:undefined不是app.get(“ /”)上的函数

  23. 23

    了解控制台错误“TypeError: undefined is not an object”和“Uncaught TypeError: Cannot read property '0' of undefined”

  24. 24

    typeerror undefined 不是 node js 中的函数

  25. 25

    如何解决 Uncaught TypeError: Cannot read property 'call' of undefined?

  26. 26

    显示“Uncaught TypeError: Can not read property 'length' of undefined”错误的原因是在 JavaScript 中

  27. 27

    为什么我会收到 TypeError: Cannot read property 'length' of undefined?

  28. 28

    在类构造函数中 this.button = document.getElementById('button') / Uncaught TypeError: Cannot read property 'addEventListener' of undefined?

  29. 29

    如何克服 Node.js 中的“TypeError: Class extends value undefined is not a constructor or null”错误?

热门标签

归档