我正在尝试使用node,express和multer通过入站webhook存储来自sendgrid的电子邮件。 sendgrids网站上有一个例子如下:
var express = require('express');
var multer = require('multer');
var upload = multer();
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(multer());
});
app.post('/parse', upload.array('files', 3) function (req, res) {
var from = req.body.from;
var text = req.body.text;
var subject = req.body.subject;
var num_attachments = req.body.attachments;
for (i = 1; i <= num_attachments; i++){
var attachment = req.files['attachment' + i];
// attachment will be a File object
}
});
var server = app.listen(app.get('port'), function() {
console.log('Listening on port %d', server.address().port);
});
发送带有附件的电子邮件时,此代码会引发错误。错误是“意外字段”。我假设array.upload(“files”,3)的声明是问题所在。有人解决了吗?
答案 0 :(得分:2)
您可以在没有字段名称时使用.any()来解决此问题(请参阅documentation for any()
这是一个示例代码
app.post('/parse', upload.any() function (req, res) {
var from = req.body.from;
var text = req.body.text;
var subject = req.body.subject;
var num_attachments = req.body.attachments;
for (i = 1; i <= num_attachments; i++){
var attachment = req.files['attachment' + i];
// attachment will be a File object
}
});