• Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

67
Views
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').

9 months ago · Juan Pablo Isaza
1 answers
Answer question

0

Turns out I just needed a loop, ex.;

uni.forEach(unipair => {
    console.log(unipair);
9 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.