我正在尝试解析一个大型JSON对象中的多个JSON对象。现在它将整个JSON对象存储为一个,我需要将它们解析为单独的对象以存储在mongodb中。这是我的代码。
获取订单请求
WooCommerce.get('orders', function(err, data, res){
console.log(res);
var parsedOrders = JSON.parse(res);
var step;
for(step = 0; step < parsedOrders.length; step++){
var emagispaceOrders = new Orders({orders: step});
emagispaceOrders.save();
}
});
用于在mongoose模式中存储JSON对象的模型
var Schema = mongoose.Schema;
var orderSchema = new Schema({
orders: {type: Object}
});
var Orders = mongoose.model('Orders', orderSchema);
module.exports = Orders;
它保存订单对象,但不保存JSON对象中的数据。所以基本上总结一下,我的mongodb特别填充了8个空对象,这些对象只包含下面显示的内容,而不是JSON对象发送的实际数据。
{ "_id" : ObjectId("58d93d05fdcd524224469604"), "orders" : 0, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469606"), "orders" : 2, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469605"), "orders" : 1, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469607"), "orders" : 3, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469608"), "orders" : 4, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469609"), "orders" : 5, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960a"), "orders" : 6, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960b"), "orders" : 7, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960d"), "orders" : 9, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960c"), "orders" : 8, "__v" : 0 }
何时应包含通过woocommerce提交的完整订单,其中包含订单ID,总计,客户名称等等。
任何帮助将不胜感激。
编辑///////
mongoose schema
var Schema = mongoose.Schema;
var orderSchema = new Schema({
id: {type: Number},
parent_id: {type: Number},
status: {type: String},
number: {type: Number},
customer_id: {type: Number},
shipping_total: {type: String},
total: {type: String},
total_tax: {type: String}
});
var Orders = mongoose.model('Orders', orderSchema);
module.exports = Orders;
订购JSON示例
{"id":6190,"parent_id":0,"status":"processing","order_key":"wc_order_58cc462aa2f9d","number":6190,"currency":"USD","version":"2.6.14","prices_include_tax":false,"date_created":"2017-03-17T20:25:00","date_modified":"2017-03-17T20:25:49","customer_id":1,"discount_total":"0.00","discount_tax":"0.00","shipping_total":"814.50","shipping_tax":"72.04","cart_tax":"361.54","total":"5335.59","total_tax":"433.58","billing":{"first_name":"John","last_name":"Doe","company":"Emagispace","address_1":"123 Billing Street","address_2":"Apt. #1","city":"Boulder","state":"CO","postcode":"80301","country":"US","email":"bt_buyer_us@paypal.com","phone":"312-123-4567"},"shipping":{"first_name":"John","last_name":"Doe","company":"Emagispace","address_1":"123 Billing Street","address_2":"Apt. #1","city":"Boulder","state":"CO","postcode":"80301","country":"US"},"payment_method":"cheque","payment_method_title":"Our team will send you an invoice for products ordered.","transaction_id":"","customer_ip_address":"75.166.79.141","customer_user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/56.0.2924.87 Safari\/537.36","created_via":"checkout","customer_note":"","date_completed":"2017-03-17T20:25:49","date_paid":"","cart_hash":"bf2aeb9eba51a119baeb896cb778093d","line_items":[{"id":207,"name":"3' Emagiblock","sku":"2000003A","product_id":5924,"variation_id":0,"quantity":22,"tax_class":"","price":"96.88","subtotal":"2131.25","subtotal_tax":"188.51","total":"2131.25","total_tax":"188.51","taxes":[{"id":1,"total":"188.5091","subtotal":"188.5091"}],"meta":[]},{"id":208,"name":"2' Emagiblock","sku":"2000002A","product_id":5920,"variation_id":0,"quantity":28,"tax_class":"","price":"52.49","subtotal":"1469.72","subtotal_tax":"130.00","total":"1469.72","total_tax":"130.00","taxes":[{"id":1,"total":"129.9967","subtotal":"129.9967"}],"meta":[]},{"id":209,"name":"1' Emagiblock","sku":"2000001A","product_id":5916,"variation_id":0,"quantity":10,"tax_class":"","price":"33.66","subtotal":"336.60","subtotal_tax":"29.77","total":"336.60","total_tax":"29.77","taxes":[{"id":1,"total":"29.7723","subtotal":"29.7723"}],"meta":[]},{"id":210,"name":"EmagiClip","sku":"4500001A","product_id":5984,"variation_id":0,"quantity":12,"tax_class":"","price":"12.50","subtotal":"149.94","subtotal_tax":"13.26","total":"149.94","total_tax":"13.26","taxes":[{"id":1,"total":"13.2622","subtotal":"13.2622"}],"meta":[]}],"tax_lines":[{"id":212,"rate_code":"US-CO-CO TAX-1","rate_id":"1","label":"CO Tax","compound":false,"tax_total":"361.54","shipping_tax_total":"72.04"}],"shipping_lines":[{"id":211,"method_title":"Weight Based Shipping","method_id":"main","total":"814.50","total_tax":"72.04","taxes":[{"id":1,"total":"72.042525"}]}],"fee_lines":[],"coupon_lines":[],"refunds":[],"_links":{"self":[{"href":"https:\/\/www.emagispace.com\/wp-json\/wc\/v1\/orders\/6190"}],"collection":[{"href":"https:\/\/www.emagispace.com\/wp-json\/wc\/v1\/orders"}],"customer":[{"href":"https:\/\/www.emagispace.com\/wp-json\/wc\/v1\/customers\/1"}]}}
这是一个JSON响应对象。我收到的响应包含其中的35个,我需要从包含较小订单对象的主/大JSON对象中解析这些对象。
答案 0 :(得分:0)
对象不是valid Mongoose schema types,这意味着您需要事先知道订单对象的形状,或者Mongoose不会保存它们。
修改强>
为了回应您的编辑,我发现您并没有尝试保存正确的对象。你在做什么
Orders({orders: step});
其中step
是一个索引,因此您可以有效地保存一个数字。你想要做的是
Orders({orders: parsedOrders[step]});
这将引用正确的顺序。
编辑2
实际上,您的架构在原始问题和编辑中有所不同。无论哪种方式,请记住,要获得订单,您需要parsedOrders[step]
引用它,其中step
是索引。