question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'img' of undefined

See original GitHub issue

Hello, I try to upload a simple file but i got an erreor TypeError: Cannot read property ‘img’ of undefined.

here is my route.js :

var express = require('express'),
router = express.Router(),
home = require('../controllers/home'),
image = require('../controllers/image'),
multer = require('multer'),
path = require('path'),
upload=multer({ dest: path.join(__dirname,'../public/upload/temp')});


module.exports = function(app) {
router.get('/', home.index);
router.get('/images/:image_id', image.index);
router.post('/images',upload.single('img'),  image.create);
router.post('/images/:image_id/like', image.like);
router.post('/images/:image_id/comment', image.comment);
app.use(router);
};

and my form :

<form method="post" action="/images" enctype="multipart/formdata">
<div class="panel-body form-horizontal">
<div class="form-group col-md-12">
<label class="col-sm-2 control-label" for="file">Parcourir:</label>
<div class="col-md-10">
<input class="form-control" type="file" name="img" id="file">
</div>
</div>

my image controller :

create: function(req, res) {
var saveImage = function() {
    var possible = 'abcdefghijklmnopqrstuvwxyz0123456789',
imgUrl = '';
for(var i=0; i < 6; i+=1) {
imgUrl += possible.charAt(Math.floor(Math.random() *
possible.length));
}

console.log(req.body);
var tempPath = req.file.img.path,
ext = path.extname(req.file.img.name).toLowerCase(),
targetPath = path.resolve('./public/upload/' + imgUrl + ext);
.... 

please if someone can help me

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
LinusUcommented, Mar 20, 2016

Since you are using .single() the file will be available at simply req.file, not req.file.img, could you try and change to that?

0reactions
tinaroootcommented, Mar 15, 2020

解决了吗

Read more comments on GitHub >

github_iconTop Results From Across the Web

I got this error => TypeError: Cannot read property 'image' ...
I got this error => TypeError: Cannot read property 'image' of undefined · It seems like product is undefined. Can you console log...
Read more >
TypeError: Cannot read property '_img' of undefined #258
I am attempting to resize images during nuxt.generate by calling context.$img from a server plugin. The plugin is intentionally naïve and ...
Read more >
cannot read properties of undefined (reading 'image')
Here's an example of a JavaScript TypeError: Cannot read property of undefined thrown when a property is attempted to be read on an...
Read more >
Uncaught TypeError: Cannot read property of undefined In
Uncaught TypeError: Cannot read property of undefined error occurs in Chrome when you read a property or call a method on an undefined...
Read more >
Uncaught TypeError: Cannot read property 'message' of ...
Uncaught TypeError: Cannot read property 'message' of undefined ... Already disabled all Plugins and switched to another theme. ... Hello @ psychosis51! I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found