How to get data from an object visible on browser console but returns null when I manually traverse it on my code?

谁都会走 提交于 2021-02-05 09:29:16

问题


//listen for messages
channel.on("messageAdded", function (message) {
    //render new message    
    console.log("New awesome message", message)
    //this is null for some reason
    console.log("Stringify 3",JSON.stringify(message.state.aggregatedDeliveryReceipt))
}

But by storing it as a global object via console right click,

I am able to do a console.log(temp1.state.aggregatedDeliveryReceipt)

and get back

The problem is that, when I use the same state.aggregatedDeliveryReceipt on my JS file, I am unable to get the desired result. It says that it is null

After googling some, I learned it was some sort of constructor...

Firebase: getting a weird `e` object when performing a get

What I have tried:

  1. used JSON.stringify() on temp1

Result: Uncaught TypeError: Converting circular structure to JSON --> starting at object with constructor 'l' | property '_fsm' -> object with constructor 'o' --- property 'context' closes the circle

  1. used it on temp1.state

Result:

{"sid":"IMd0XXXXXXXfd8aa","index":532,"author":"XXXX","body":"XXXX","timestamp":"2020-XXXXX:12.XX","dateUpdated":"2020-11-XXXXX:12.574Z","lastUpdatedBy":null,"attributes":{},"type":"text","media":null,"participantSid":"MB0a5d2XXXXXXX27ddf4da6","aggregatedDeliveryReceipt":null}
  1. used it on temp1.state.aggregatedDeliveryReceipt and is also returned null

  2. Directly doing it on console Result: it worked but I am unable to replicate it on my JS file. For some reason, the console can see it directly but not through my code


回答1:


It is likely a timing issue if you are getting different behavior in the devtools console vs in your code. The data may well have loaded by the time you try to access it in the devtools console, whereas it likely hasn't if you are accessing it too soon in your code.

As a first step, use setTimeout() to defer the code reading the value by a decent amount of time (a few seconds) and then see if it can be read correctly. Then at least you will know what the problem is.



来源:https://stackoverflow.com/questions/64640478/how-to-get-data-from-an-object-visible-on-browser-console-but-returns-null-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!