Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5850437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:08:25+00:00 2026-05-22T13:08:25+00:00

This is the code (refer to CODE) I use when I try to create

  • 0

This is the code (refer to CODE) I use when I try to create new CouchDB document. The document contains one or more existing CouchDB documents (history tracking). The resulting json (refer to JSON) for the newly created doc is something like:

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 []. 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. Any suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T13:08:26+00:00Added an answer on May 22, 2026 at 1:08 pm

    The problem is just timing. The function you pass to getJSON isn’t called until the web server returns a response to your request. So item will not be defined immediately after the call to getJSON. Adding the alert calls just slows things down so that the asynchronous call has time to complete.

    Try moving the items.push(item) line inside the getJSON callback.

    Edit

    To make things clear, try this (in Chrome or Firefox with Firebug open, so that you have a proper console.log function):

    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);
        }
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
currently I have this client code in my PHP MVC web app: try {
In IntelliJ 10.0.3 I use the menu option new web service and this generates
This code in JS gives me a popup saying i think null is a
This code is from Prototype.js . I've looked at probably 20 different tutorials, and
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
This code always works, even in different browsers: function fooCheck() { alert(internalFoo()); // We
This code does not seem to compile, I just need to write something to
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
This code is executed by many way. When it's executed by the form button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.