我有一个如此定义的猫鼬模型:
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
}]
})
})
我正在尝试将Quickbooks API中的JSON数据插入Mongo。 我使用以下代码来实现它:
//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++;
}
}
JSON看起来像这样: 这是截断版本。
{ 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' },
我运行我的代码,我在日志中收到此错误:
[TypeError: Cannot use 'in' operator to search for '_id' in 2505.0]
我需要弄清楚如何以模型指定的格式存储ColData
。就此而言,我不知道我错在哪里 - 我用来创建文档的模型或代码。
这就是console.log(vbd)
的样子:
{ 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: [] }
保罗的回答使我朝着正确的方向前进,并使用以下代码实现了我想要的目标:
//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++;
}
}
答案 0 :(得分:1)
您的对象vbd
看起来超出了范围。您在嵌套循环内调用var vbd = new vbDetail(...)
,当您调用.save()