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.

What is the right way to use MemoryStorage option?

See original GitHub issue

Hi.

I get an error with this code(Cannot read property 'buffer'). What is the right way to use memoryStorage option?

//  import and use the modules
var app   = require('express')();
var http = require('http').Server(app);
var bodyParser = require("body-parser");
var multer  = require('multer');
var storage = multer.memoryStorage();
var upload = multer({ storage: storage });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.post('/imagenes', upload.single(''), function(req,res){

        req.file.buffer;     // TypeError: Cannot read property 'buffer' of undefined

});

http.listen(8080,function(){
    console.log("Listening: 8080");
});

Thanks.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
geetayucommented, Sep 6, 2017

Hi @LinusU var express = require(“express”) var multer = require(‘multer’) var app = express() var path = require(‘path’) var fs = require(‘fs’)

var ejs = require(‘ejs’) app.set(‘view engine’, ‘ejs’)

var MAGIC_NUMBERS = { jpg: ‘ffd8ffe0’, jpg1: ‘ffd8ffe1’, png: ‘89504e47’, gif: ‘47494638’ }

function checkMagicNumbers(magic) { if (magic == MAGIC_NUMBERS.jpg || magic == MAGIC_NUMBERS.jpg1 || magic == MAGIC_NUMBERS.png || magic == MAGIC_NUMBERS.gif) return true }

app.get(‘/api/file’, function(req, res) { res.render(‘index’) })

app.post(‘/api/file’, function(req, res) { var upload = multer({ storage: multer.memoryStorage() }).single(‘userFile’) upload(req, res, function(err) { var buffer = req.file.buffer var magic = buffer.toString(‘hex’, 0, 4) var filename = req.file.fieldname + ‘-’ + Date.now() + path.extname(req.file.originalname) if (checkMagicNumbers(magic)) { fs.writeFile(‘./uploads/’ + filename, buffer, ‘binary’, function(err) { if (err) throw err res.end(‘File is uploaded’) }) } else { res.end(‘File is no valid’) } }) })

var port = process.env.PORT || 8080 app.listen(port, function() { console.log('Node.js listening on port ’ + port) })

Can you please help me on this i am getting below error “TypeError: multer.memoryStorage is not a function”

2reactions
LinusUcommented, Aug 11, 2015

I think that this line is your problem:

app.post('/imagenes', upload.single(''), function(req,res){
//                                  ^ here

It shouldn’t be an empty string, but rather the name of the field that is going to be uploaded.

What does your form look like?

Read more comments on GitHub >

github_iconTop Results From Across the Web

multer.memoryStorage JavaScript and Node.js code ...
Best JavaScript code snippets using multer.memoryStorage(Showing top 5 results out of 315) ; multer.memoryStorage({ destination: function(req, file, callback) { ...
Read more >
Uploading sensitive data, Should I user memoryStorage ...
The data will then be saved to mySQL. All is done in Node.js. Now I wonder what is the most secure way to...
Read more >
Multer buffer is null when using memoryStorage
I like doing it this way so that I can have more control over the error message. var uploadFile = multer( { storage:...
Read more >
Express multer middleware
MemoryStorage. The memory storage engine stores the files in memory as Buffer objects. It doesn't have any options. const ...
Read more >
Apache Hadoop 3.3.4 – Memory Storage Support in HDFS
The choice of RAM Disk allows better data persistence across Data Node process restarts. The following setup will work on most Linux ...
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