Why is this object property undefined?

后端 未结 2 1642
Happy的楠姐
Happy的楠姐 2021-02-20 08:15

Consider the code below. The first console.log correctly logs the image, and you can see its properties in the image below. However, when I try logging one if its p

2条回答
  •  离开以前
    2021-02-20 08:37

    I've solved the problem. Basically, the object in question (that.data[0].cards) has its properties created by a function a() that runs after all the AJAX requests for the necessary XML files have been processed. I allow the requests to run asynchronously, using a counter to determine in the success callback function if a() should be called yet.

    After a() runs, function b() is supposed to perform operations on that.data[i].cards. However, b() was running prior to a() being called because of a()'s reliance on the asynchronous requests. So the solution was simply to make a() call b().

    So this turned out to be a pretty simple mistake on my part. What made it so confusing was the fact that logging that.data[0].cards to the console showed me that in fact the cards object had already been built, when in fact it had not yet. So the console was providing me with incorrect--or at least unclear--information.

    Thanks for everyone's help last night! Upvotes all around :)

提交回复
热议问题