TypeError:无法读取未定义的属性“缓冲区”

Posted

技术标签:

【中文标题】TypeError:无法读取未定义的属性“缓冲区”【英文标题】:TypeError: Cannot read property 'buffer' of undefined 【发布时间】:2019-01-20 12:44:08 【问题描述】:

我的代码:

var express = require("express");
var bodyParser = require("body-parser");
var multer = require("multer");
var fs = require("fs");
var ejs = require('ejs');
var app = express();

var upload = multer( dest: 'uploads/' )

app.set('view engine', 'ejs');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded( extended: true ));

app.use(function(req, res, next) 
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
);

app.get('/', function(req, res)
  console.log('Works');
  res.render('index');
);

app.post('/', upload.single('test'), function (req, res, next) 
  var raw = new Buffer(req.file.buffer.toString(), 'base64');

  fs.writeFile('./uploads/upload.png', raw, function (err) 
    if (err) 
      console.log('Error');
      return next(err);
    
    res.end('Success!');
  );
);

var server = app.listen(3000, function() 
console.log("Listening on port %s...", server.address().port););

我的错误:

TypeError: Cannot read property 'buffer' of undefined
    at /Users/jacob/nodejs-prj/polyglotdev-test/app.js:27:33
    at Layer.handle [as handle_request] (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/route.js:137:13)
    at multerMiddleware (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/multer/lib/make-middleware.js:18:41)
    at Layer.handle [as handle_request] (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/layer.js:95:5)
    at /Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/jacob/nodejs-prj/polyglotdev-test/node_modules/express/lib/router/index.js:335:12)

我必须从 rasperry pi 接收 base64 格式的图像文件。然后我必须将其解码回图像并将其保存在 mongo db 中。 我试图用邮递员将一些测试数据发送到我的端点。但总是会出现这个错误。 在 Postman 中,它看起来像这样: Screenshot of postman

【问题讨论】:

您是否在表单上输入了enctype="multipart/form-data" 【参考方案1】:

我解决了这个问题。忘记我的'index.ejs'文件,有一个标签''。属性 'name="test"' 必须等于 "upload.single('test')" 中的参数。

【讨论】:

【参考方案2】:

试试这个:

var upload = multer(
    dest: 'uploads/',
    storage: multer.memoryStorage()
);

【讨论】:

以上是关于TypeError:无法读取未定义的属性“缓冲区”的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:无法读取未定义的属性(读取“名称”)

TypeError:无法读取未定义的属性“findAll”(expressjs)

TypeError:无法读取未定义的属性(读取“问题”)

TypeError:无法读取未定义的属性“babel”

TypeError:无法读取未定义的属性(读取“匹配”):

TypeError:无法读取未定义的属性“存在”