NodeJS:检索包装在字符串中的数组

时间:2018-10-05 08:45:49

标签: javascript node.js express mailgun

我目前正在尝试保存一个邮件对象(),我正在从API返回到我们的Mongo数据库中。 对于所有传递的值,接受“附件”字段,这都很好。附件应该是一个对象数组,但是它包装在一个字符串中。现在的问题是:如何从该字符串中获取数组?

我从POST请求中获得的请求正文的示例:

  

{时间戳:“ 1538728432”,令牌:   '0a1336769443d5a593d0ef7c1c817d29ef64ba422fc3cc45f1',签名:   'c44c67660f0ad97bbb5ece92c733302d4511d4607b5787ae6aa8f2203b0e2e3e',
  域:“ karaton.uk”,发件人:“”,
  'X-Envelope-From':'',To:'',   'Dkim-Signature':'v = 1; a = rsa-sha256;   c =放松/放松; \ td = karaton.be;   s = 20140924; \ th = content-type:subject:reply-to:from:to:message-id:mime-version:date:from; \ tbh = Xfm + LiX4NVMKTRaQB7gNUXf6z8yVjUqn23aenMee / q4 =; \ tb = JEslFgX + EO9 oXJH5DGsXALaGa98ki9o5tLddmEcClqhZ8IJLR0 + vrMJ3x \ t   zU5ThqHNElHpt6VE9MX5ZSHJHnI2kiEXq7MN5BUWfStlS2stYwooZcuZKOkbZXRaUHODewUQbhvZQD \ t   5 / eA5RltbAOqwIaA =',来自:“”,“用户代理”:   'One.com网络邮件27.8.5',日期:'星期五,2018年10月5日10:21:49 +0200',
  '邮件ID':'<1538727709180.51726.8212@webmail12>',
  “哑剧版本”:“ 1.0”,已收到:[“来自   mailrelay4-2.pub.mailoutpod1-cph3.one.com   (mailrelay4-2.pub.mailoutpod1-cph3.one.com [46.30.212.3])通过   具有ESMTP ID 5bb71f1f.7f030c2a1580-smtp-in-n02的mxa.mailgun.org;星期五   2018年10月5日08:21:51 -0000(UTC)',        '来自onecom-webmail2 94.143.189.242(未知[46.30.211.130])\ tby mailrelay4.pub.mailoutpod1-cph3.one.com(哈隆)   与ESMTPSA \ tid b4d6415f-c877-11e8-86b7-d0431ea8bb10; \ tFri,10月5日   2018 08:21:49 +0000(UTC)'],'X-Halone-Cookie':   'c9ea0f7e80128b5bfaf5bf8d8aca6fced5bcb162',主题:'2   附件”,“ message-url”:   'https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=',   'Reply-To':'',收件人:'tho@karaton.uk',
  发件人:“ thomas.l@karaton.be”,“ X-Mailgun-Incoming”:“是”,
  'message-headers':'[[“ X-Mailgun-Incoming”,“是”],   [“ X-信封-来自”,“”],[“已接收”,“来自   mailrelay4-2.pub.mailoutpod1-cph3.one.com   (mailrelay4-2.pub.mailoutpod1-cph3.one.com [46.30.212.3])通过   具有ESMTP ID 5bb71f1f.7f030c2a1580-smtp-in-n02的mxa.mailgun.org;星期五   2018年10月5日08:21:51 -0000(UTC)“],[” Dkim签名“,” v = 1;   a = rsa-sha256; c =放松/放松; \ td = karaton.be;   s = 20140924; \ th = content-type:subject:reply-to:from:to:message-id:mime-version:date:from; \ tbh = Xfm + LiX4NVMKTRaQB7gNUXf6z8yVjUqn23aenMee / q4 =; \ tb = JEslFgX + EO9 oXJH5DGsXALaGa98ki9o5tLddmEcClqhZ8IJLR0 + vrMJ3x \ t   zU5ThqHNElHpt6VE9MX5ZSHJHnI2kiEXq7MN5BUWfStlS2stYwooZcuZKOkbZXRaUHODewUQbhvZQD \ t   5 / eA5RltbAOqwIaA =“],[” X-Halone-Cookie“,   “ c9ea0f7e80128b5bfaf5bf8d8aca6fced5bcb162”],[“ X-Halone-Id”,   “ b4d6415f-c877-11e8-86b7-d0431ea8bb10”],[“已收到”,“来自   onecom-webmail2 94.143.189.242(未知[46.30.211.130])\ tby   具有ESMTPSA \ tid的mailrelay4.pub.mailoutpod1-cph3.one.com(Halon)   b4d6415f-c877-11e8-86b7-d0431ea8bb10; \ tFri,2018年10月5日08:21:49   +0000(UTC)“],[” X-Originating-Ip“,” 94.143.189.242“],[” User-Agent“,” One.com网络邮件27.8.5“],[”日期“,”星期五, 2018年10月05日10:21:49   +0200“],[” Mime-Version“,” 1.0“],[” Message-Id“,” <1538727709180.51726.8212@webmail12>“],[”收件人“,”“],   [“发件人”,“”],[“回复至”,   “”],[“主题”,“ 2个附件”],   [“内容类型”,“多部分/混合;   boundary = \“ ---------- 8210-1538727709180-1 \”“]]','X-Halone-Id':   'b4d6415f-c877-11e8-86b7-d0431ea8bb10','内容类型':   '多份/混合; boundary =“ ---------- 8210-1538727709180-1”',
  “ X-Originating-Ip”:“ 94.143.189.242”,主题:“ 2个附件”,
  附件:'[{“ url”:   “ https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/0”,   “内容类型”:“应用程序/ pdf”,“名称”:“ WOORDZOEKER maken _在线   _达到oplossing2 copy.pdf”,“大小”:53398},{“ url”:“ https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/1”,   “内容类型”:   “ application / vnd.openxmlformats-officedocument.spreadsheetml.sheet”,   “ name”:“ Geschatte uren.xlsx”,“ size”:44210}]','body-plain':   '\ r \ n \ r \ n','body-html':'
\ r \ n','stripped-html':   '
\ n','stripped-text':'','stripped-signature':''   }

2 个答案:

答案 0 :(得分:0)

使用内置的JSON.parse()函数,它将按需返回对象数组。它与JSON.stringify()相反,后者则相反。所以基本上 JSON.parse(req.body.attachments)应该可以解决问题。

答案 1 :(得分:0)

JSON.parse应该可以解决问题

const response = {
  timestamp: '1538728432',
  token: '0a1336769443d5a593d0ef7c1c817d29ef64ba422fc3cc45f1',
  Subject: '2 attachments',
  attachments: '[{"url": "https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/0", "content-type": "application/pdf", "name": "WOORDZOEKER maken _ Online _ Met oplossing2 copy.pdf", "size": 53398}, {"url": "https://se.api.mailgun.net/v3/domains/karaton.uk/messages/eyJwIjpmYWxzZSwiayI6IjEyNzQ1ZmViLTM4OTItNDdkZC04MTJiLWJlYzU4ZjNjYTI4MCIsInMiOiI1NDE1ODQ0ZjIyIiwiYyI6InRhbmtiIn0=/attachments/1", "content-type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "name": "Geschatte uren.xlsx", "size": 44210}]'
}

const attachments = JSON.parse(response.attachments);
console.log(attachments[0]);
console.log(attachments[1]);

参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse