javascript - Having trouble inserting documents in mongoDB using mongoose and node.js -
javascript - Having trouble inserting documents in mongoDB using mongoose and node.js -
i have mongoose model defined 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 trying insert json info quickbooks api mongo. using next code accomplish it:
//test for(var row in report["rows"]["row"]){ while(count < companies){ //console.log(report.rows.row[count].header.coldata[0].value); // save rows corresponding 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 this: 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 gunnchamberlain pl' }, { value: '' }, { value: '' }, { value: '' }, { value: '2505.0' }, { value: '2505.0' }, { value: '' } ] }, type: 'section' },
i run code , error in log:
[typeerror: cannot utilize 'in' operator search '_id' in 2505.0]
i need figure out how coldata
stored in format specified model. matter, don't know going wrong- model or code utilize create documents.
this 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 reply led me in right direction , achieved want using next code:
//test for(var row in report["rows"]["row"]){ while(count < companies){ //console.log(report.rows.row[count].header.coldata[0].value); // save rows corresponding 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 row info 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 record db vbd.save(function(err){ if(err) console.log(err); }) } count++; } }
looks object vbd
out of scope. phone call var vbd = new vbdetail(...)
within nested loop, out of scope when phone call .save()
javascript json node.js mongodb
Comments
Post a Comment