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

jQuery: Problem populating array when calling $.getJSON

本文关键字:数组 问题 填充 jQuery 调用 getJSON      更新时间:2023-09-26

这是我尝试创建新的CouchDB文档时使用的代码(参考代码)。该文档包含一个或多个现有的 CouchDB 文档(历史记录跟踪)。新创建的文档生成的 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"
     }
   ]
}

法典:
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);
  });

问题和问题是:当下面检查 2 行被编码时,生成的 json 文档的"项目"部分是 []。当警报(item.manufacturer)下面的检查2未被注释时,我收到内容为"未定义"的警报,但是生成的json文档的"项目"部分已正确设置,就像示例中所示一样。有什么建议吗?

问题只是时机。在 Web 服务器返回对您的请求的响应之前,不会调用您传递给 getJSON 的函数。因此,item不会在调用getJSON后立即定义。添加alert调用只会减慢速度,以便异步调用有时间完成。

尝试在getJSON回调中移动items.push(item)行。

编辑

为了清楚起见,请尝试以下操作(在打开Firebug的Chrome或Firefox中,以便您具有适当的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);
    }
  });
});