0

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++;
                }


            }
4
  • 3
    Would be nice if the expert downvoting the question explains why it was downvoted, so I can actually not make the same mistake again. Commented Oct 22, 2014 at 22:07
  • By the looks of the error it sounds like the document you are trying to save is not valid. Can you do a console.log of vdb before saving so we can see what the model looks like before it is being saved Commented Oct 23, 2014 at 0:38
  • I agree, there's nothing wrong with how you asked the question, and you provided more detail than many. Commented Oct 23, 2014 at 15:39
  • Thanks @Paul for your answer and the upvote. Your answer is the correct one. I realized that what I want to achieve is to save an array of objects. I have updated the model to reflect that. Commented Oct 23, 2014 at 16:26

1 Answer 1

1

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()

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I updated the question with the code that ended up doing what I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.