'Cannot access object from GraphQL query (JavaScript)
Subgraph query returns results to console, but my javascript module can't access any values from it, returning an error:
TypeError: Cannot read properties of undefined (reading 'symbol')
According to everything I read online, this is how to access object properties.
quni.mjs:
import axios from 'axios'
export async function quni() {
const uniData = async () => {
var result;
try{
result = await axios.post(
'https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-v3-polygon',
{
query:`
{
pools(
first: 2,
where: {
liquidity_gt: 100
}
)
{
id
token1{symbol}
token0{symbol}
}
}
`
}
);
} catch(error){
console.error(error);
}
const unipairs = result.data.data.pools;
//console.log(unipairs);
return result.data.data.pools;
}
return uniData();
}
quni();
quniparse.mjs:
import { quni } from './quni.mjs';
async function unicheck() {
var uni = await quni();
var unitoken0 = uni.token0.symbol;
var unitoken1 = uni.token1.symbol;
var ticker = toString([unitoken0+"/"+unitoken1]);
console.log(ticker)
};
unicheck();
Solutions I've tried:
JSON.stringify(unipairs)
to try accessing using JSON objects in quniparse.mjs, but that yields identical results.
fs.writeFile('./unipairs', JSON.stringify(unipairs), null, 2)
to see if accessing properties from .json file might help.
All yield identical results (cannot read 'symbol').
Solution 1:[1]
Turns out I just needed a loop, ex.;
uni.forEach(unipair => {
console.log(unipair);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | the_prophecy |