Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从NodeJSON中获取数据会导致未定义_Json_Node.js_Woocommerce_Woocommerce Rest Api - Fatal编程技术网

从NodeJSON中获取数据会导致未定义

从NodeJSON中获取数据会导致未定义,json,node.js,woocommerce,woocommerce-rest-api,Json,Node.js,Woocommerce,Woocommerce Rest Api,所以今天我写了一个nodejs应用程序来从一个网站的API中获取数据。这是我的代码: var processing = WooCommerce.get('orders?status='+type, function(err, data, res) { var result = res; JSON.stringify(result) console.log(result); result = result[0].meta_data; console.log(result);

所以今天我写了一个nodejs应用程序来从一个网站的API中获取数据。这是我的代码:

var processing = WooCommerce.get('orders?status='+type, function(err, data, 
res) {
  var result = res;
  JSON.stringify(result)
  console.log(result);
  result = result[0].meta_data;
  console.log(result);
  });
这是我的控制台日志:抱歉搞砸了

    [{"id":2977,"parent_id":0,"number":"2977","order_key":"wc_order_5a8bc4c350d54","created_via":"checkout","version":"3.0.5","status":"on-hold","currency":"INR","date_created":"2018-02-20T12:18:3
5","date_created_gmt":"2018-02-20T06:48:35","date_modified":"2018-02-20T12:18:41","date_modified_gmt":"2018-02-20T06:48:41","discount_total":"0.00","discount_tax":"0.00","shipping_total":"0.00
","shipping_tax":"0.00","cart_tax":"0.00","total":"40.00","total_tax":"0.00","prices_include_tax":false,"customer_id":342,"customer_ip_address":"103.104.77.159","customer_user_agent":"mozilla\
/5.0 (linux; android 6.0.1; le x526 build\/iixosop5801910121s) applewebkit\/537.36 (khtml, like gecko) chrome\/64.0.3282.137 mobile safari\/537.36","customer_note":"","billing":{"first_name":"
Fahad","last_name":"Khan","company":"","address_1":"","address_2":"","city":"Delhi","state":"DL","postcode":"","country":"IN","email":"shimail786@gmail.com","phone":"8745076002"},"shipping":{"
first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"paytm-qr","payment_method_title":"Pay with Paytm QR"
,"transaction_id":"","date_paid":null,"date_paid_gmt":null,"date_completed":null,"date_completed_gmt":null,"cart_hash":"0119311d11c4978ecc7bf6f59b53586f","meta_data":[{"id":91320,"key":"_billi
ng_stl","value":"https:\/\/steamcommunity.com\/tradeoffer\/new\/?partner=452464312&token=Gq27CMGc"},{"id":91321,"key":"billing_stl","value":"https:\/\/steamcommunity.com\/tradeoffer\/new\/?par
tner=452464312&token=Gq27CMGc"},{"id":91324,"key":"_woocs_order_rate","value":"1"},{"id":91325,"key":"_woocs_order_base_currency","value":"INR"},{"id":91326,"key":"_woocs_order_currency_change
d_mannualy","value":"0"}],"line_items":[{"id":1641,"name":"MAG-7 | Silver (Factory New)","product_id":2972,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"40.00","subtotal_tax":"0.00"
,"total":"40.00","total_tax":"0.00","taxes":[],"meta_data":[],"sku":"","price":40}],"tax_lines":[],"shipping_lines":[],"fee_lines":[],"coupon_lines":[],"refunds":[],"_links":{"self":[{"href":"
https:\/\/ezpz-skins.com\/wp-json\/wc\/v2\/orders\/2977"}],"collection":[{"href":"https:\/\/ezpz-skins.com\/wp-json\/wc\/v2\/orders"}],"customer":[{"href":"https:\/\/ezpz-skins.com\/wp-json\/w
c\/v2\/customers\/342"}]}},{"id":2976,"parent_id":0,"number":"2976","order_key":"wc_order_5a8bc2fabf6d8","created_via":"checkout","version":"3.0.5","status":"on-hold","currency":"INR","date_cr
eated":"2018-02-20T12:10:58","date_created_gmt":"2018-02-20T06:40:58","date_modified":"2018-02-20T12:11:02","date_modified_gmt":"2018-02-20T06:41:02","discount_total":"0.00","discount_tax":"0.
00","shipping_total":"0.00","shipping_tax":"0.00","cart_tax":"0.00","total":"95.00","total_tax":"0.00","prices_include_tax":false,"customer_id":342,"customer_ip_address":"103.104.77.159","cust
omer_user_agent":"mozilla\/5.0 (linux; android 6.0.1; le x526 build\/iixosop5801910121s) applewebkit\/537.36 (khtml, like gecko) chrome\/64.0.3282.137 mobile safari\/537.36","customer_note":""
,"billing":{"first_name":"Fahad","last_name":"Khan","company":"","address_1":"","address_2":"","city":"Delhi","state":"DL","postcode":"","country":"IN","email":"shimail786@gmail.com","phone":"
8745076002"},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"paytm-qr","payment_method_
title":"Pay with Paytm QR","transaction_id":"","date_paid":null,"date_paid_gmt":null,"date_completed":null,"date_completed_gmt":null,"cart_hash":"ca6b663ea1f4b4c7ed65b9fd39acc2cb","meta_data":
[{"id":91268,"key":"_billing_stl","value":"https:\/\/steamcommunity.com\/tradeoffer\/new\/?partner=452464312&token=1m7SCUVf"},{"id":91269,"key":"billing_stl","value":"https:\/\/steamcommunity.
com\/tradeoffer\/new\/?partner=452464312&token=1m7SCUVf"},{"id":91272,"key":"_woocs_order_rate","value":"1"},{"id":91273,"key":"_woocs_order_base_currency","value":"INR"},{"id":91274,"key":"_w
oocs_order_currency_changed_mannualy","value":"0"}],"line_items":[{"id":1639,"name":"SG 553 | Tiger Moth (Field Tested)","product_id":911,"variation_id":0,"quantity":1,"tax_class":"","subtotal
":"42.00","subtotal_tax":"0.00","total":"42.00","total_tax":"0.00","taxes":[],"meta_data":[],"sku":"","price":42},{"id":1640,"name":"Glock-18 | Bunsen Burner (Factory New)","product_id":532,"v
ariation_id":0,"quantity":1,"tax_class":"","subtotal":"53.00","subtotal_tax":"0.00","total":"53.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":4861,"key":"_woocs_order_rate","value":"1"}
,{"id":4862,"key":"_woocs_order_base_currency","value":"INR"},{"id":4863,"key":"_woocs_order_currency_changed_mannualy","value":"0"}],"sku":"","price":53}],"tax_lines":[],"shipping_lines":[],"
fee_lines":[],"coupon_lines":[],"refunds":[],"_links":{"self":[{"href":"https:\/\/ezpz-skins.com\/wp-json\/wc\/v2\/orders\/2976"}],"collection":[{"href":"https:\/\/ezpz-skins.com\/wp-json\/wc\
/v2\/orders"}],"customer":[{"href":"https:\/\/ezpz-skins.com\/wp-json\/wc\/v2\/customers\/342"}]}}]
undefined
因此,在阅读了大量有关StackOverflow的问题后,我意识到我的数据是一个数组。这就是我添加result=result[0]的原因;但这给了我一个未定义的注意,它在日志的末尾。如果我删除.meta_数据,它只返回[,第一个字符


我错在哪里?我对所有这些都不熟悉,还在学习,所以请解释:

res是字符串格式的,所以不要使用JSON.stringify使用JSON.parse,这样它将被转换回Javascript对象,然后尝试如下所示的结果,然后尝试访问元数据

var processing = WooCommerce.get('orders?status='+type, function(err, data, 
    res) {
      var result = JSON.parse(res);
      console.log(result[0]);
      result = result[0].meta_data;
      console.log(result);
});

删除JSON.stringify部分,因为您正在将JavaScript对象更改为字符串。如果这是一段真正的代码-您没有将stringify分配给任何内容,请尝试删除它并添加:result=JSON.parseresult;@Sindis我尝试过删除它,但仍然会导致未定义。它很有效!非常感谢!但我以前已经尝试过,我记得它崩溃了d给我一个关于obj的错误。