The results are that I can parse and console.log the ‘assets’ and the ‘contract’, but I cannot arrive at displaying the ‘templateId’ as it is ‘undefined’
It looks like you are printing obj2.contracts.templateId instead of obj2.contracts[0].templateId. templateId is a property on each contract not on the array of contracts.
See my above message for all the console logs. The contract is not empty.
If I apply the same statement from a static Const with exactly the same JSON it works, but for some reason for a object that comes from the stream it doesn’t
Are you sure you get the same log after making the changes? Looking at your code I’m also a bit confused as to why you first call JSON.stringify only to then call JSON.parse again.
Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse ()
at Default (Default.js:32)
at renderWithHooks (react-dom.development.js:14803)
at mountIndeterminateComponent (react-dom.development.js:17482)
at beginWork (react-dom.development.js:18596)
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
at invokeGuardedCallback (react-dom.development.js:292)
at beginWork$1 (react-dom.development.js:23203)
at performUnitOfWork (react-dom.development.js:22154)
at workLoopSync (react-dom.development.js:22130)
at performSyncWorkOnRoot (react-dom.development.js:21756)
at scheduleUpdateOnFiber (react-dom.development.js:21188)
at updateContainer (react-dom.development.js:24373)
at react-dom.development.js:24758
at unbatchedUpdates (react-dom.development.js:21903)
at legacyRenderSubtreeIntoContainer (react-dom.development.js:24757)
at Object.render (react-dom.development.js:24840)
at Module…/src/index.js (index.js:16)
at webpack_require (bootstrap:785)
at fn (bootstrap:150)
at Object.1 (index.js:28)
at webpack_require (bootstrap:785)
at checkDeferredModules (bootstrap:45)
at Array.webpackJsonpCallback [as push] (bootstrap:32)
at main.chunk.js:1
@bartcant Don’t forget that the result of a useQuery is a QueryResult. One needs to make sure that it is not loading and that the number of obj.contracts.length > 0 before you access a contract payload. Those things will change once the effect gets executed by React and the underlying fetch is resolved.
I have a sneaky suspicion that the errors you are seeing are due to the timing of when you see the results from the useQuery hook. Remember in that in Javascript
>> var x = []
Array []
>> x[0]
undefined
>> x[0].foo
TypeError: x[0] is undefined
Dealing with these confusing states makes me long for the safety of effect tracking monads.
To be safe, try assets.contracts.map(c => c.payload). As @Leonid_Rozenberg said I suspect it’s called twice once while the list is still loading and it’s empty and then once the entry is there.