0

I am submitting an array that contains nested arrays from Angular to a server running Node.js. I would like to parse each field so I can include it in a confirmation email. I'm not sure how to get the key/values from the array and put them in the email body.

I've tried var email = req.body.email for the items one level deep and var cost = req.body.detail.cost for the nested items two levels deep, but those don't seem to work.

Any suggestions?

Here's the Javascript:

router.post('/', function(req, res) {
  if (err) {
   res.status(402).send(err.message);
}

else {
  console.log(JSON.stringify(req.body, null, 2));
  var data = {
    from: storefront@example.com,
    to: email,
    subject: 'Your Order Confirmation',
    text: 'Thank you for your order. Below is your invoice:\n\n'+
          'Name: '+ firstName + ' ' + lastName +
          'Address: ' + address + ', ' + city + ', ' + state.code +'\n\n'+

          'Item    Description     Cost\n\n'+
          item +   description  +  cost 
// want to add the item, description, and cost for each item ordered
// . . . 

var mailgun = new Mailgun({apiKey: api_key, domain: domain});

  mailgun.messages().send(data, function (err, body) {

    if (err) {

      console.log('error');
    }
   else {

      console.log('email sent!');

    }
  })
}; 

This is what the array looks like this:

[
  [
    {
     "firstName": "John",
     "lastName": "Doe",
     "address": "555 Broadway",
     "city": "New York",
     "email": "john@example.com",
     "phone": "2125551212",
     "state": {
       "code": "NY",
       "state": "New York"
    },
     "timestamp": {
       "addedAt": 1443911047642
   },
    "zip": "10001",
    "$id": "-K-jnWa0IBiGCAInbaIr",
  }
],
[
  {
    "item": "Pants",
    "description": "Wranglers",
    "timestamp": {
      "addedAt": 1443911007264
   },
    "detail": {
      "cost": 60,
      "size": "36"
   },
    "$id": "-K-jnMj64LS-XMM18J-8",
},
{
   "item": "Pants",
   "description": "Levi's",
   "timestamp": {
     "addedAt": 1443911018026
  },
  "detail": {
    "cost": 80,
    "size": "33"
  },
   "$id": "-K-jnPMJDLJMKIH4rAUe",

  }
 ]
]
Ken
  • 3,091
  • 12
  • 42
  • 69
  • 2
    `req.body` isn't "flattened" so any keys are immediately available. You'll have to access them from within the arrays -- `req.body[0][0].email`, etc. [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Jonathan Lonowski Oct 04 '15 at 00:02

1 Answers1

0

I have had same problem....this will solve it.

var cartContent = req.body.slice(1); 

//produces new array sans user contact information assuming you pushed it to the front of the array at some point....please do so!

Access properties like this for user contact content

req.body[0].name

For the cart items, it's much easier to loop through or manipulate cartContent sans the UN-releated user contact info object.

cartContent[0].item
deek
  • 1,085
  • 1
  • 9
  • 27