frishi
frishi

Reputation: 882

Having trouble inserting documents in mongoDB using mongoose and node.js

I have a mongoose model defined like so:

module.exports = mongoose.model('vbDetail', {

company_name: String,
rowsdata: {vals:
            {
                date: Date,
                transaction_type: String,
                transaction_num: String,
                due_date: Date,
                amount: Number,
                open_balance: Number,
                balance: Number
            }
        },

meta_rows: [{
    identifier: String,
    processing_date: Date,
    processing_amount :Date,
    notes: String
}]


})


})

I am trying to insert JSON data from the Quickbooks API into Mongo. I am using the following code to achieve it:

 //test
 for(var row in report["Rows"]["Row"]){
     while(count < companies){
        //console.log(report.Rows.Row[count].Header.ColData[0].value);
        // save the rows corresponding to each client
        for(var rowdata in report.Rows.Row[count].Rows.Row){
           for(var coldata in report.Rows.Row[count].Rows.Row[rowdata].ColData){
              var vbd = new vbDetail({
                  company_name: report.Rows.Row[count].Header.ColData[0].value,
                  rowsdata: report.Rows.Row[count].Rows.Row[rowdata].ColData[coldata].value

              });
           }
        }


       vbd.save(function(err){
         if(err) console.log(err);
       })

       count++;
      }

}

The JSON looks like this: This is the truncated version.

  { Header: 
       { ColData: 
          [ { value: 'GunnChamberlain PL' },
            { value: '' },
            { value: '' },
            { value: '' },
            { value: '' },
            { value: '' },
            { value: '' } ] },
      Rows: 
       { Row: 
          [ { ColData: 
               [ { value: '03/10/2014' },
                 { value: 'Bill' },
                 { value: '2341' },
                 { value: '03/10/2014' },
                 { value: '500.0' },
                 { value: '500.0' },
                 { value: '500.0' } ],
              type: 'Data' },
            { ColData: 
               [ { value: '04/30/2014' },
                 { value: 'Bill' },
                 { value: '4663' },
                 { value: '04/30/2014' },
                 { value: '450.0' },
                 { value: '450.0' },
                 { value: '950.0' } ],
              type: 'Data' },
            { ColData: 
               [ { value: '05/31/2014' },
                 { value: 'Bill' },
                 { value: '4878' },
                 { value: '05/31/2014' },
                 { value: '875.0' },
                 { value: '875.0' },
                 { value: '1825.0' } ],
              type: 'Data' },
            { ColData: 
               [ { value: '06/30/2014' },
                 { value: 'Bill' },
                 { value: '5115' },
                 { value: '06/30/2014' },
                 { value: '680.0' },
                 { value: '680.0' },
                 { value: '2505.0' } ],
              type: 'Data' } ] },
      Summary: 
       { ColData: 
          [ { value: 'Total for GunnChamberlain PL' },
            { value: '' },
            { value: '' },
            { value: '' },
            { value: '2505.0' },
            { value: '2505.0' },
            { value: '' } ] },
      type: 'Section' },

I run my code and I get this error in the log:

[TypeError: Cannot use 'in' operator to search for '_id' in 2505.0]

I need to figure out how to get the ColData stored in the format specified by the model. For that matter, I don't know where I am going wrong- the model or the code I use to create documents.

This is what console.log(vbd) looks like:

{ company_name: 'GS & CO',
_id: 54491e60dbd6350000000033,
meta_rows: [],
rowsdata: [] }

{ company_name: 'GunnChamberlain PL',
_id: 54491e60dbd635000000004f,
meta_rows: [],
rowsdata: [] }

{ company_name: 'Simple Group, Inc.',
_id: 54491e60dbd635000000005d,
meta_rows: [],
rowsdata: [] }

{ company_name: 'SM Inc.',
_id: 54491e60dbd6350000000079,
meta_rows: [],
rowsdata: [] }

{ company_name: 'Think Holdings',
_id: 54491e60dbd63500000000cd,
meta_rows: [],
rowsdata: [] }

UPDATE

Paul's answer led me in the right direction and I achieved what I want using the following code:

 //test
            for(var row in report["Rows"]["Row"]){
                while(count < companies){
                    //console.log(report.Rows.Row[count].Header.ColData[0].value);
                     // save the rows corresponding to each client
                     for(var rowdata in report.Rows.Row[count].Rows.Row){
                        for(var coldata in report.Rows.Row[count].Rows.Row[rowdata].ColData){

                           // save company name
                           var vbd = new vbDetail({
                                company_name: report.Rows.Row[count].Header.ColData[0].value

                            });

                            }

                            // save the row data per company
                            vbd.rowsdata = ({vals:{
                                                    date: report.Rows.Row[count].Rows.Row[rowdata].ColData[0].value,
                                                    transaction_type: report.Rows.Row[count].Rows.Row[rowdata].ColData[1].value,
                                                    transaction_num: report.Rows.Row[count].Rows.Row[rowdata].ColData[2].value,
                                                    due_date: report.Rows.Row[count].Rows.Row[rowdata].ColData[3].value,
                                                    amount: report.Rows.Row[count].Rows.Row[rowdata].ColData[4].value,
                                                    open_balance: report.Rows.Row[count].Rows.Row[rowdata].ColData[5].value,
                                                    balance: report.Rows.Row[count].Rows.Row[rowdata].ColData[6].value

                                                 }
                                             })
                            console.log(vbd);
                            // Save the record to DB
                            vbd.save(function(err){
                                if(err) console.log(err);
                            })
                        }



                   count++;
                }


            }

Upvotes: 0

Views: 95

Answers (1)

Paul
Paul

Reputation: 36349

Looks like your object vbd is out of scope. You call var vbd = new vbDetail(...) inside the nested loop, which is out of scope when you call .save()

Upvotes: 1

Related Questions