Sendgrid入站webhook与节点和multer

我试图通过入站webhook使用node,express和multer来存储来自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)的声明是问题所在。 有没有人解决这个问题?

你可以通过使用.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 } });