Olusegun Odumosu Olusegun Odumosu - 1 year ago 51
Javascript Question

S3 Nodejs TypeError: Cannot read property 'path' of undefined

I am implementing this in Nodejs to upload files (photos, mp3, pdf) to amazon web services s3. Currently am trying to upload an mp3 file but I keep getting tis error: "TypeError: Cannot read property 'path' of undefined". I am using the Multipart middlemware.

var fs = require('fs');
//S3FS instance
var S3FS = require('s3fs')
//multiparty middleware (to upload our file)----> S3FS will read it's object and stream it to AWS
//nmp install connect-multiparty

var multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty();

var express = require('express');

var route = express.Router();
module.exports = route;
route.use(multipartyMiddleware);

//create new filesystem
var s3fsImpl = new S3FS('unasbeatz', {
accessKeyId: 'xxxxxxx',
secretAccessKey: 'xxxxx'
});

//create bucket ----> unasbeatz
s3fsImpl.create();

route.post('/', function (req, resp) {
//get the file from our request object (from multiparty object, it sets the request object)
var file = req.files.file;
//create new readstream
var stream = fs.createReadStream(file.path);
//write to AWS using stream
return s3fsImpl.writeFile(file.originalFileName, stream).then(function () {
fs.unlink(file, function (err) {
if (err)
console.error(err);
})
//else we write uploaded
console.log('uploaded');
})
})

route.get('/', function (req, resp) {
resp.json({ SecretData: 'abcde' })
});


var stream: grabs direct part of every uploaded file. But I don't know why the error comes up

error:

TypeError: Cannot read property 'path' of undefined
at /Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/app/routes/mp3_route.js:34:42
at Layer.handle [as handle_request] (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/layer.js:95:5)
at /Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:330:12)
at next (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:271:10)
at multipart (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/connect-multiparty/index.js:50:53)
at Layer.handle [as handle_request] (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:312:13)
at /Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:330:12)
at next (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:271:10)
at Function.handle (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:176:3)
at router (/Users/AzeezOlusegunOdumosu/Desktop/unasbeatz/node_modules/express/lib/router/index.js:46:12)

Answer Source

What I finally implemented was to updload my files in AWS and save their urls in MongoDB,I then reuest for these urls in my web application to show these files in the browser

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download