Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
Getting "TypeError: cannot read properties of undefined" but I'm getting 0 undefined values when I console.log

I have this function which is supposed to return then name of an item collection_name that exists within a users account. In this case the user is called hello. This api is return an object in this case so I'm turning it into an array using object.values. What am I doing wrong here? (also, the "hi" that's being returned is just a place holder, I intend to actually return the collection_name there.

import Spinner from '../Spinner';
import { useState, useEffect } from 'react';

function Badges() {
  const [assets, setAssets] = useState<any[]>();

  useEffect(() => {
    async function fetchData() {
      const res = await fetch(
        'https://proton.api.atomicassets.io/atomicassets/v1/accounts/hello'
      );
      const { data } = await res.json();
      setAssets(data);
    }
    fetchData();
  }, []);

  if (!assets) {
    return (
      <div>
        <Spinner />
      </div>
    );
  }

  const messageArray = Object.values(assets);

  return (
    <ul>
      {messageArray.map((result) => {
        const {
          0: {
            collection: { collection_name },
          },
        } = result;

        console.log(collection_name);

        return <>hi</>;
      })}
    </ul>
  );
}

export default Badges;

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Give this a shot. I think this is what you wanted? Might be completely off though :) Anyways, some notable changes were

  • Removed const messageArray = Object.values(assets);
  • Reworked the .map inside the return

You almost had it, although I think a big part of the confusion was caused by how the data was coming back from the API. The structure of the data seemed slightly confusing....at least to me. To avoid this issue in the future, you can always back your API responses with a TypeScript interface.

import Spinner from '../Spinner';
import { useState, useEffect } from 'react';


interface Assets { 
  collections: CollectionParent[];
  templates: Template[];
  assets: string;
}

interface Template {
  collection_name: string;
  template_id: string;
  assets: string;
}

interface CollectionParent {
  assets: string;
  collection: Collection;
}

interface Collection {
  allow_notify: boolean;
  author: string;
  authorized_accounts: string[];
  collection_name: string;
  contract: string;
  created_at_block: string;
  created_at_time: string;
  img: string;
  market_fee: number;
  name: string;
}

function Badges() {
  const [assets, setAssets] = useState<Assets>();

  useEffect(() => {
    async function fetchData() {
      const res = await fetch(
        "https://proton.api.atomicassets.io/atomicassets/v1/accounts/hello"
      );
      const { data } = await res.json();
      setAssets(data);
    }
    fetchData();
  }, []);

  if (!assets) {
    return (
      <div>
        <Spinner />
      </div>
    );
  }

  return (
    <ul>
      {assets?.collections?.length > 0 &&
        assets.collections.map((collectionParent, index) => {
          return (
            <li key={index}>{collectionParent.collection.collection_name}</li>
          );
        })}
    </ul>
  );
}

export default Badges;

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda