Parsing the results of a 'useQuery'
- bartcantOP 1
I am experimenting with parsing the results from a ‘useQuery’
Here is my code
const assets = useQuery(Main.CitizenInvitation);
const obj = JSON.stringify(assets);
con> sole.log ("asset : " + obj);
const obj2 = JSON.parse(obj);
console.log ("obj contracts : " + JSON.stringify(obj2.contracts[0]));
console.log ("templateId : " + JSON.stringify(obj2.contracts.templateId));Here is the console.log output:
asset : {“contracts”:[{“templateId”:“5930f9c206352112718aaa4bf0104f368d4b4763cf201e055073cdea03bc3301:Main:CitizenInvitation”,“contractId”:“00bbb0f3c6bd9a27d8ffe304bc6aca8b0cc058eea3e483704df0469be9776fa9c5”,“signatories”:[“Operator”],“observers”:[“Alice”],“agreementText”:“”,“payload”:{“operator”:“Operator”,“citizen”:“Alice”,“roletype”:“Citizen”}}],“loading”:false}
obj contracts : {“templateId”:“5930f9c206352112718aaa4bf0104f368d4b4763cf201e055073cdea03bc3301:Main:CitizenInvitation”,“contractId”:“00bbb0f3c6bd9a27d8ffe304bc6aca8b0cc058eea3e483704df0469be9776fa9c5”,“signatories”:[“Operator”],“observers”:[“Alice”],“agreementText”:“”,“payload”:{“operator”:“Operator”,“citizen”:“Alice”,“roletype”:“Citizen”}}
templateId : undefined
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’
Any suggestions ?
- cocreature 1
It looks like you are printing
obj2.contracts.templateIdinstead ofobj2.contracts[0].templateId.templateIdis a property on each contract not on the array of contracts. - bartcant 1
- cocreature 1
That looks like
contractsis empty and thereforecontracts[0]is undefined. Do you see any output from the secondconsole.logstatement? - bartcant
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 - cocreature
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.stringifyonly to then callJSON.parseagain. - bartcant
I agree that it would be much simpler to do just a JSON.parse but it creates errors
const assets = useQuery(Main.CitizenInvitation);
//const obj = JSON.stringify(assets);
//console.log ("asset : " + obj);
const obj2 = JSON.parse(assets);
console.log ("obj contracts : " + JSON.stringify(obj2.contracts[0]));Creates the following error :
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
If you are available… I can do a screenshare.
- cocreature
You shouldn’t need either
JSON.parseorJSON.stringify. Use the result directly:const assets = useQuery(…); console.log(assets.contracts); - bartcant
How can I get to the details in the contracts ? I need to retrieve a specific value ? How do I get to that value ?
- bartcant
bartcant:obj contracts : {“templateId”:“5930f9c206352112718aaa4bf0104f368d4b4763cf201e055073cdea03bc3301:Main:CitizenInvitation”,“contractId”:“00bbb0f3c6bd9a27d8ffe304bc6aca8b0cc058eea3e483704df0469be9776fa9c5”,“signatories”:[“Operator”],“observers”:[“Alice”],“agreementText”:“”,“payload”:{“operator”:“Operator”,“citizen”:“Alice”,“roletype”:“Citizen”}}
In the example above , I need to retrieve the “roletype” value
- cocreature
It’s a JSON object, you can just use
.fieldto drill down soassets.contracts[0].payload.roletype. - bartcant
console.log ("roletype : " + JSON.stringify(assets.contracts[0]));
displays
roletype : {“templateId”:“5930f9c206352112718aaa4bf0104f368d4b4763cf201e055073cdea03bc3301:Main:CitizenInvitation”,“contractId”:“00bbb0f3c6bd9a27d8ffe304bc6aca8b0cc058eea3e483704df0469be9776fa9c5”,“signatories”:[“Operator”],“observers”:[“Alice”],“agreementText”:“”,“payload”:{“operator”:“Operator”,“citizen”:“Alice”,“roletype”:“Citizen”}}
console.log ("roletype : " + JSON.stringify(assets.contracts[0].payload));
displays
`console.log ("roletype : " + JSON.stringify(assets.contracts.payload));
displays
roletype : undefined
console.log ("roletype : " + assets.contracts[0].payload);
displays
- Leonid_Rozenberg
@bartcant Don’t forget that the result of a useQuery is a QueryResult. One needs to make sure that it is not
loadingand that the number ofobj.contracts.length > 0before you access a contract payload. Those things will change once the effect gets executed by React and the underlyingfetchis 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
useQueryhook. Remember in that in Javascript>> var x = [] Array [] >> x[0] undefined >> x[0].foo TypeError: x[0] is undefinedDealing with these confusing states makes me long for the safety of effect tracking monads.
- cocreature
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. - bartcant
Issue Solved

const QueryResult = useQuery(Main.CitizenInvitation);
const assets = QueryResult.contracts.map(c => c.payload.roletype);
const roletype = JSON.stringify (assets);
console.log ("Roletype : " + roletype)Thanks @cocreature @Leonid_Rozenberg !!!


