最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - .map() through array of object in next.js does not display in html but in console? - Stack Overflow

programmeradmin5浏览0评论

I am not able to display the elements of this array in html. I am fetching data from the Bscscan api. I can fetch perfectly from the first api but the second does not show the data in the local browser. I will show you all the code I've got.

I added also pictures of what the browser looks like and my terminal. It looks like i cannot map through arrays with objects, but I have tried everything change the object into react readable content, and nothing worked, so I will show you the code I started with. The Address 2 is the problem address.

Code :

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import React from 'react'


export default function Home(props) {



  const datatransone  =  props.addresstransaction;
  const databalone = props.addressbalance;

  //something wrong with stringify
  // const datatransone = JSON.stringify(datatransjson);

  console.log(databalone);
  console.log(datatransone);

  return  (
    <ul>      
      <h1>Address One</h1>
      {databalone.map((balance) => {
        return (
      <li>{(balance.result * 1e-18).toString()}</li>
      )})}
      <div>
      <h1>Address Two</h1>
      <div>
      {datatransone.map(function(d){
         return (<li key={d.hash}>{d.result.hash}</li>)
       })}
      </div>
      </div>
    </ul>
  );
}


export async function getServerSideProps(context) {

    let wlunarush = ['0xb4e856c2a257457b862daed14967578bbdba3526', '0x52ab04656c807a3af96f98e4428291e813c2687a'];
    // let contractaddress = '0xde301D6a2569aEfcFe271B9d98f318BAee1D30a4';
  

    let balance = wlunarush.map(function(element){

      let bscbalance = ';action=tokenbalance&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&tag=latest&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';

    return bscbalance;
  })

    let transaction = wlunarush.map(function(element){

      let bsctransaction = ';action=tokentx&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&page=1&startblock=0&offset=1&endblock=999999999&sort=asc&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';

    return bsctransaction;
  })



    const addressbalance = await Promise.all(balance.map(u => fetch(u)))
    const addresstransaction = await Promise.all(transaction.map(e => fetch(e)))


  // console.log(bscbalancelist)
  // console.log(bscbalancelist);
  // const responsesec = await fetch(transaction);
  // const bscbalancelist = await bscbalone.json();


  return {
    props: {
      addressbalance:  await Promise.all(addressbalance.map(r => r.json())),
      addresstransaction:  await Promise.all(addresstransaction.map(p => p.json())),
    }
  };


}

I am not able to display the elements of this array in html. I am fetching data from the Bscscan api. I can fetch perfectly from the first api but the second does not show the data in the local browser. I will show you all the code I've got.

I added also pictures of what the browser looks like and my terminal. It looks like i cannot map through arrays with objects, but I have tried everything change the object into react readable content, and nothing worked, so I will show you the code I started with. The Address 2 is the problem address.

Code :

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import React from 'react'


export default function Home(props) {



  const datatransone  =  props.addresstransaction;
  const databalone = props.addressbalance;

  //something wrong with stringify
  // const datatransone = JSON.stringify(datatransjson);

  console.log(databalone);
  console.log(datatransone);

  return  (
    <ul>      
      <h1>Address One</h1>
      {databalone.map((balance) => {
        return (
      <li>{(balance.result * 1e-18).toString()}</li>
      )})}
      <div>
      <h1>Address Two</h1>
      <div>
      {datatransone.map(function(d){
         return (<li key={d.hash}>{d.result.hash}</li>)
       })}
      </div>
      </div>
    </ul>
  );
}


export async function getServerSideProps(context) {

    let wlunarush = ['0xb4e856c2a257457b862daed14967578bbdba3526', '0x52ab04656c807a3af96f98e4428291e813c2687a'];
    // let contractaddress = '0xde301D6a2569aEfcFe271B9d98f318BAee1D30a4';
  

    let balance = wlunarush.map(function(element){

      let bscbalance = 'https://api-testnet.bscscan./api?module=account&action=tokenbalance&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&tag=latest&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';

    return bscbalance;
  })

    let transaction = wlunarush.map(function(element){

      let bsctransaction = 'https://api-testnet.bscscan./api?module=account&action=tokentx&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&page=1&startblock=0&offset=1&endblock=999999999&sort=asc&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';

    return bsctransaction;
  })



    const addressbalance = await Promise.all(balance.map(u => fetch(u)))
    const addresstransaction = await Promise.all(transaction.map(e => fetch(e)))


  // console.log(bscbalancelist)
  // console.log(bscbalancelist);
  // const responsesec = await fetch(transaction);
  // const bscbalancelist = await bscbalone.json();


  return {
    props: {
      addressbalance:  await Promise.all(addressbalance.map(r => r.json())),
      addresstransaction:  await Promise.all(addresstransaction.map(p => p.json())),
    }
  };


}

Console.log in Terminal :

Browser :

Share Improve this question edited Apr 6, 2022 at 14:59 Loco2343 asked Apr 6, 2022 at 14:04 Loco2343Loco2343 471 gold badge1 silver badge6 bronze badges 3
  • d.result is an array of objects, not an object. You can't access the hash value without specifying which element in the array you want to use like d.result[0].hash – Brian Thompson Commented Apr 6, 2022 at 14:55
  • Please do not edit your question with the answer. If you solved the problem you can post an answer to your own question below. – Brian Thompson Commented Apr 6, 2022 at 14:58
  • Oh ok will do it – Loco2343 Commented Apr 6, 2022 at 14:59
Add a ment  | 

2 Answers 2

Reset to default 3

You have a nested array, so you will need to use a nested map in order to iterate over all items.

I've simplified a bit, but you should be able to get the idea and adapt it to match your exact needs.

function Home() {

  const databalone = [{status: '1', message: 'OK', result: '1234'},{status: '1', message: 'OK', result: '1234'}];
  const datatransone = [{status: '1', message: 'OK', result: [{hash: '4321'}]},{status: '1', message: 'OK', result: [{hash: '54321'}]}];

  return  (
    <ul>      
      <h1>Address One</h1>
      {databalone.map((balance) => {
        return (
      <li>{(balance.result * 1e-18).toString()}</li>
      )})}
      <div>
      <h1>Address Two</h1>
      <div>
      {datatransone.map(function(d){
         return (<li>{d.result.map((r) => <span>{r.hash}</span>)}</li>)
       })}
      </div>
      </div>
    </ul>
  );
}

ReactDOM.render(<Home />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare./ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="root"></div>

Solved, I had to use the index to specify what array I wanted to .map().

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import React from 'react'


export default function Home(props) {



  const datatransone  =  props.addresstransaction;
  const databalone = props.addressbalance;

  var transactionarray = datatransone[0];
  // var newgotwo = datatransone[1];




  // console.log(databalone);
  // console.log(datatransone);

  return  (
    <ul>      
      <h1>Address One</h1>
      {databalone.map((balance) => {
        return (
      <li>{(balance.result * 1e-18).toString()}</li>
      )})}
      <div>
      <h1>Address Two</h1>
      <div>
        {transactionarray.result.map(function(d){
         return (<li key={d.hash}>{d.hash}</li>)
       })}
        {/* {newgotwo.result.map(function(d){
         return (<li key={d.hash}>{d.hash}</li>)
       })} */}
      </div>
      </div>
    </ul>
  );
}


export async function getServerSideProps(context) {

    let wlunarush = ['0xb4e856c2a257457b862daed14967578bbdba3526', '0x52ab04656c807a3af96f98e4428291e813c2687a'];
    // let contractaddress = '0xde301D6a2569aEfcFe271B9d98f318BAee1D30a4';
  

    let balance = wlunarush.map(function(element){

      let bscbalance = 'https://api-testnet.bscscan./api?module=account&action=tokenbalance&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&tag=latest&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';

    return bscbalance;
  })

    let transaction = wlunarush.map(function(element){

      let bsctransaction = 'https://api-testnet.bscscan./api?module=account&action=tokentx&contractaddress=0x0DBfd812Db3c979a80522A3e9296f9a4cc1f5045&address=' + element + '&page=1&startblock=0&offset=1&endblock=999999999&sort=asc&apikey=E2JAJX6MAGAZUHSYIBZIEMKMNW9NZKPR7I';

    return bsctransaction;
  })



    const addressbalance = await Promise.all(balance.map(u => fetch(u)))
    const addresstransaction = await Promise.all(transaction.map(e => fetch(e)))


  // console.log(addresstransaction)
  // console.log(addressbalance);
  // const responsesec = await fetch(transaction);
  // const bscbalancelist = await bscbalone.json();


  return {
    props: {
      addressbalance:  await Promise.all(addressbalance.map(r => r.json())),
      addresstransaction:  await Promise.all(addresstransaction.map(p => p.json())),
    }
  };


}

发布评论

评论列表(0)

  1. 暂无评论