Javascript Express JS路由器中间件-Body Parser-req.Body有连字符
我正在接收来自Mailgun的解析电子邮件,这些邮件通过他们的API发布到我设置的URL端点。接收HTTP帖子的URL是使用主体解析器中间件到MongoDB的Expressjs路由。对于“sender”这样的简单文本键,我的http post到MongoDB的路由工作正常,但是我对一些包含连字符的消息参数的格式有问题。例如“人体素”。如果我包含参数“req.body.body plain”,Express将抛出一个错误。有人有工作吗 我不希望对整个字符串进行正则化 以下是正在发布的电子邮件响应的示例:Javascript Express JS路由器中间件-Body Parser-req.Body有连字符,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,我正在接收来自Mailgun的解析电子邮件,这些邮件通过他们的API发布到我设置的URL端点。接收HTTP帖子的URL是使用主体解析器中间件到MongoDB的Expressjs路由。对于“sender”这样的简单文本键,我的http post到MongoDB的路由工作正常,但是我对一些包含连字符的消息参数的格式有问题。例如“人体素”。如果我包含参数“req.body.body plain”,Express将抛出一个错误。有人有工作吗 我不希望对整个字符串进行正则化 以下是正在发布的电子邮件响应的
recipient: 'postmaster@appmail.sisdaf.com',
sender: 'kevsdaf@mail.com',
subject: 'tewsgs',
from: 'Kevin Psdit <kesdfit@gmail.com>',
'X-Envelope-From': '<kasdftit@gmail.com>',
'body-plain': 'this is a test\r\n',
Received:
[ 'from mail-qk0-f179.google.com (mail-qk0-f179.google.com [209.85.220.179]) by mxa.mailgun.org with ESMTP id 556bfda1.7f7658358ef0- in01; Mon, 01 Jun 2015 06:37:21 -0000 (UTC)',
'by qkx62 with SMTP id 62so79143349qkx.3 for <postmaster@appmail.simadsftrade.com>; Sun, 31 May 2015 23:37:21 -0700 (PDT)',
'by 10.140.18.241 with HTTP; Sun, 31 May 2015 23:37:21 -0700 (PDT)' ],
'Dkim-Signature': 'v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; cdx2K5lDwCjwcy0S6407m6/q9tAnFIltsT48O1nCACzQ4RQQYiXa VuiA==',
'Mime-Version': '1.0',
'X-Received': 'by 10.55.23.130 with SMTP id 2mr35323631qkx.33.1433140641295; Sun, 31 May 2015 23:37:21 -0700 (PDT
)',Date: 'Sun, 31 May 2015 23:37:21 -0700'
解决方法是使用括号表示法,这样您可以访问具有点表示法中无效字符的键
req.body['body-plain']
额外详细信息:如果您使用的是猫鼬模型,并且它也有连字符,那么您就是这样做的 假设mongoose模型是这样的,请注意名称中带有连字符的属性: 使用mongoose模型时,您的文档应如下所示:
谢谢你的快速回答。
req.body['body-plain']
const augmentedWritingTextSchema = new mongoose.Schema({
text: {
type: String,
required: true,
unique: false,
},
name: {
type: String,
required: true,
},
'profile-id': {
type: String,
required: true,
},
'profile-type': {
type: String,
required: true,
},
'company-wide-visibility': {
type: String,
required: true,
},
});
augmentedWritingTextDocument = new AugmentedWritingText({
text: reqObj.text,
name: reqObj.name,
'profile-id': reqObj['profile-id'],
'profile-type': reqObj['profile-type'],
'company-wide-visibility': reqObj['company-wide-visibility'],
});