richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 261 forks source link

server.js example not finding object methods #234

Closed gb53smith closed 4 years ago

gb53smith commented 4 years ago

Trying to use express-fileupload. I can see the object using console.log but cannot access methods like name and mv

"devDependencies": {
      "@types/express": "^4.17.6",
      "@types/node": "^8.0.29",
      "typescript": "3.3.3333"
   },
   "dependencies": {
      "@types/express-fileupload": "^1.1.3",
      "@types/jquery": "^3.5.0",
      "body-parser": "^1.18.1",
      "express": "^4.15.4",
      "express-fileupload": "^1.1.7-alpha.4",
      "pug": "^3.0.0",
      "reflect-metadata": "^0.1.13",
      "sqlite3": "^4.2.0",
      "typeorm": "^0.2.25"
   },

I tried the example server.js and get an error trying to find the .name method

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

const PORT = 8000;
app.use('/form', express.static(__dirname + '/index.html'));

// default options
app.use(fileUpload());

app.get('/ping', function(req, res) {
  res.send('pong');
});

app.post('/upload', function(req, res) {
  let sampleFile;
  let uploadPath;

  if (!req.files || Object.keys(req.files).length === 0) {
    res.status(400).send('No files were uploaded.');
    return;
  }

  console.log('req.files >>>', req.files); // eslint-disable-line

  sampleFile = req.files.sampleFile;
  console.log(sampleFile.name)

  uploadPath = __dirname + '/uploads/' + sampleFile.name;

  sampleFile.mv(uploadPath, function(err) {
    if (err) {
      return res.status(500).send(err);
    }

    res.send('File uploaded to ' + uploadPath);
  });
});

app.listen(PORT, function() {
  console.log('Express server listening on port ', PORT); // eslint-disable-line
});

Console output:

req.files >>> {
  upload: {
    name: 'import.html',
    data: <Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 21 2d 2d 20 73 61 76 65 64 20 66 72 6f 6d 20 75 72 6c 3d 28 30 30 33 38 29 68 
74 74 70 3a 2f 2f 31 ... 1112 more bytes>,
    size: 1162,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'text/html',
    md5: 'e3c93100f429d5fdfc810cce221bca41',
    mv: [Function: mv]
  }
}
TypeError: Cannot read property 'name' of undefined
    at X:\TypeScript\Recipes\src\server.js:27:26
    tempFilePath: '',
    truncated: false,
    mimetype: 'text/html',
    md5: 'e3c93100f429d5fdfc810cce221bca41',
    mv: [Function: mv]
  }
}
TypeError: Cannot read property 'name' of undefined
    at X:\TypeScript\Recipes\src\server.js:27:26
    at Layer.handle [as handle_request] (X:\TypeScript\Recipes\node_modules\express\lib\router\layer.js:95:5)
    at next (X:\TypeScript\Recipes\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (X:\TypeScript\Recipes\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (X:\TypeScript\Recipes\node_modules\express\lib\router\layer.js:95:5)
    at X:\TypeScript\Recipes\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (X:\TypeScript\Recipes\node_modules\express\lib\router\index.js:335:12)
    at next (X:\TypeScript\Recipes\node_modules\express\lib\router\index.js:275:10)
    at X:\TypeScript\Recipes\node_modules\express-fileupload\lib\processMultipart.js:140:9
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

I can see .name and other methods but cannot access them?

EDIT

I got the above Javascript working but not my Typscript implementation. Error finding .name method:

  Property 'name' does not exist on type 'UploadedFile[]'.

I thought that "@types/express-fileupload": "^1.1.3", was supposed to take care of this?

RomanBurunkov commented 4 years ago

It works ok in pure js. Doesn't tested with ts...probably u should try to address this to the '@types/express-fileupload": "^1.1.3' package author.