0

I have an object that Looks something Like this, which I would like to save onto my MongoDb Database. I am having trouble looping through the itemRows array/object. Here is a sample of what I have done so far.

{
   "itemRows": [
      {
        "item_name": "Item 01",
        "item_reg_name": "advjksdnvkj",
        "item_quantity": 2,
        "item_description": "Description",
        "item_region": "CAPE TOWN"
    },
    {
        "item_name": "Item 02",
        "item_reg_name": "98687",
        "item_quantity": 23,
        "item_description": "scscsdcsd",
        "item_region": "JOHANNESBURG"
    }
   ],
  "client_name": "AA Trucks",
  "client_contact_person": "Anele",
  "client_contact_number": "021 000 0000",
  "client_contact_email": "clientemail@gmail.com",
  "client_code": "BHJHJKBHHH",
}

My Method for Saving

/*
 * ==================================================
 * Post the Sales information capture
 * ==================================================
 */

router.post('sales/', function (req, res, next) {
   var decoded = jwt.decode( req.query.token );

   User.findById( decoded.user._id, function (err, user) {

     if (err) {
        return res.status(500).json({
            title : "Not Authorised !",
            error : err
        });
    }

    var dataCapture = new Sales({
        user: user,

        client_name            : req.body.client_name,
        client_contact_person  : req.body.client_contact_person,
        client_contact_email   : req.body.client_contact_email,
        client_contact_number  : req.body.client_contact_number,

        //How do I save the 'itemRows' array/object ?
    }),
    lineItems = new Items();

    dataCapture.save(function (err, result) {
        if (err) {
             return res.status(500).json({
                 title: 'An error occurred',
                 error: err
             });
        }

        user.save();

        for (var index = 0; index < req.body.itemRows.length; index++) {
            //salesItems
            /*lineItems.save({
                sales_id    : result._id,
                item_name   : req.body.itemRows[index].item_name
            });*/
        }

        res.status(201).json({
             message: 'Data Captured Successfully !',
             obj: result
         });
    });
 });
});

Here is my Sales model Pastebin

Questions !

1.) How do I save the itemRows array ?

After saving into MongoDb, my collection/document looks like this.

enter image description here

2.) How can I display itemRows in a table along with the parent Object?

<table class="table table-hover table-striped">
        <thead>
        <tr>
          <th> Client </th>
          <th> Job Bag : </th>
          <th> Job No : </th>
          <th> Days In Que : </th>
          <th> Exit Date : </th>
          <th> Quantity : </th>
          <th> Actions : </th>
        </tr>
        </thead>
        <tbody>
        <tr *ngFor="let sale of newSales; let i = index">
          <td> {{ sale.client_name }} </td>
          <td> 763951 </td>
          <td> 62965 </td>
          <td> {{ sale.createDate | timeAgo }}</td>
          <td> {{ sale.date_to_be_finished | date:'dd-MM-yyyy' :'+0200'}} </td>
          <td> *<tree-root [nodes]="itemRows" [options]="options"></tree-root> </td>
          <td>
            <button class="btn btn-outline-primary btn-sm" (click)="configureJob()"> Configure </button>
          </td>
        </tr>

        </tbody>
  </table>

*Note

<tree-root [nodes]="itemRows" [options]="options"></tree-root>

This is comes from Angular Tree-component. I hope my question is clear.

3.) How can I

For Interest sake. My Component looks like.

How can I tie [nodes]="itemRows" into *ngFor="let sale of newSales; let i = index" Loop.

Thank you in advance.

Anele
  • 162
  • 1
  • 3
  • 21
  • if you want save array as it is then just insert complete json object. – Rahul Bisht Apr 30 '18 at 09:31
  • Welcome to Stack Overflow. The format here is **Question** and "Answer", and not "plural". You get **one** question per post, so if you have several things to ask then please post your "questions" separately. As for **adding items to an array** the question has been long answered. Please also consider that most of your other questions will also have answers already posted here, so you really should search for each answer before you post. If needed, refer to your findings from existing posts. But at any rate, in "separate questions" please. – Neil Lunn Apr 30 '18 at 09:31

0 Answers0