TypeError: Cannot read property 'img' of undefined
See original GitHub issueHello, 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:
- Created 8 years ago
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Since you are using
.single()
the file will be available at simplyreq.file
, notreq.file.img
, could you try and change to that?解决了吗