问题
//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:
- 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
- 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}
used it on
temp1.state.aggregatedDeliveryReceipt
and is also returnednull
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