简体   繁体   English

jQuery:调用$ .getJSON时问题填充数组

[英]jQuery: Problem populating array when calling $.getJSON

This is the code (refer to CODE) I use when I try to create new CouchDB document. 这是我尝试创建新的CouchDB文档时使用的代码(请参阅CODE)。 The document contains one or more existing CouchDB documents (history tracking). 该文档包含一个或多个现有的CouchDB文档(历史记录跟踪)。 The resulting json (refer to JSON) for the newly created doc is something like: 新创建的文档生成的json(指JSON)类似于:

JSON: JSON:
{
"_id": "b3360050039389801524044daf1c963c",
"_rev": "1-2a5da40ce9a191b4decc27411ec9a181",
"hodoc_type": "INCOMMING DELIVERY NOTE",
"user_from": "ANS USER",
"status": "INSTALLED ON SITE",
"category": "ASET",
"location": "MTX SPARE",
"doctype": "hodoc",
"items": [
{
"serial_number": "310310007590",
"status": "STORED IN WAREHOUSE",
"doctype": "data",
"supplier": "INTRACOM",
"po": "0",
"category": "SPARE PART",
"user_out": "IGOR JURUKOV",
"mac": "NULL",
"eqtype": "IDU",
"location": "MTX SPARE",
"location_comment": "NULL",
"date_in": "2011-05-06",
"date_out": "2011-05-06",
"prf": "0",
"part_number": "Z00-404/63.01",
"user_in": "KOTE JANAKIEVSKI",
"bar_code": "0",
"manufacturer": "INTRACOM",
"rma": "NULL",
"product_name": "PSU for IDR-SM"
},
{
"serial_number": "310407016955",
"status": "STORED IN WAREHOUSE",
"doctype": "data",
"supplier": "INTRACOM",
"po": "0",
"category": "SPARE PART",
"user_out": "IGOR JURUKOV",
"mac": "NULL",
"eqtype": "IDU",
"location": "MTX SPARE",
"location_comment": "NULL",
"date_in": "2011-05-06",
"date_out": "2011-05-06",
"prf": "0",
"part_number": "Z00-404/63.02",
"user_in": "KOTE JANAKIEVSKI",
"bar_code": "0",
"manufacturer": "INTRACOM",
"rma": "NULL",
"product_name": "PSU for IDR-SM"
}
]
}

CODE: 码:
var docids = new Array();
var items = new Array();
$('div#divitemsout').find('img.item-out-remove').each(function () {
var id = $(this).attr('attrid');
if (docids.indexOf(id) == -1) {
docids.push(docid);
}
var item = new Object;
$.getJSON('/whdb/' + id, function(data) {
$.each(data, function(key, val) {
if (key !== "_id" && key !== "_rev") {
item[key] = val;
}
});
// Check No.1
//alert(item.manufacturer);
});
// Check No.2
//alert(item.manufacturer);
items.push(item);
});

The problem and the question are: When the line bellow Check No.2 is comented, the "items" part of the resulting json document is []. 问题和问题是:当注释第2行下面的行时,生成的json文档的“项目”部分为[]。 When the alert(item.manufacturer) bellow Check No.2 is uncommented, I get alerts with contents "undefined", but the "items" part of the resulting json document is properly set, just like show in the example. 当没有对alert(item.manufacturer)下方的Check No.2进行注释时,我会收到内容为“ undefined”的警报,但是正确设置了生成的json文档的“ items”部分,如示例中所示。 Any suggestion? 有什么建议吗?

The problem is just timing. 问题只是时间。 The function you pass to getJSON isn't called until the web server returns a response to your request. 在Web服务器返回对您的请求的响应之前,不会调用传递给getJSON的函数。 So item will not be defined immediately after the call to getJSON . 因此,不会在调用getJSON之后立即定义item Adding the alert calls just slows things down so that the asynchronous call has time to complete. 添加alert调用只会减慢速度,以便异步调用有时间完成。

Try moving the items.push(item) line inside the getJSON callback. 尝试在getJSON回调内移动items.push(item)行。

Edit 编辑

To make things clear, try this (in Chrome or Firefox with Firebug open, so that you have a proper console.log function): 为了使事情更清楚,请尝试以下操作(在Chrome或Firefox中打开Firebug,以便您具有适当的console.log函数):

var docids = new Array();
var items = new Array();
var waiting = 0;
$('div#divitemsout').find('img.item-out-remove').each(function () {
  var id = $(this).attr('attrid');
  if (docids.indexOf(id) == -1) {
    docids.push(docid);
  }
  var item = new Object;
  waiting++;
  $.getJSON('/whdb/' + id, function(data) {
    waiting--;
    $.each(data, function(key, val) {
      if (key !== "_id" && key !== "_rev") {
        item[key] = val;
      }
    });
    items.push(item);
    if(waiting == 0) {
      console.log('items:', items);
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM