[英]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. 我正在尝试将Quickbooks API中的JSON数据插入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. 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' },
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. 我需要弄清楚如何以模型指定的格式存储
ColData
。 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: 这就是
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: [] }
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++;
}
}
Looks like your object vbd
is out of scope. 看起来您的对象
vbd
不在范围内。 You call var vbd = new vbDetail(...)
inside the nested loop, which is out of scope when you call .save()
您在嵌套循环内调用
var vbd = new vbDetail(...)
,而在调用.save()
时超出范围
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.