Home > Software design >  mapping the data does not work for me react js/axios
mapping the data does not work for me react js/axios

Time:06-29

I deleted the map in the return because it doesn't work. I get a error like: map is not a function. I hope someone can help me..

This is the code:

 const [text, setText] = useState([]);
  const axios = require('axios');

useEffect(() =>{
  async function fetchData() {
  const data = JSON.stringify({
    query: `query Proposals {
    proposals (
      skip: 0,
      where: {
        space_in: ["zischan.eth"],
      },
      orderBy: "created",
      orderDirection: desc
    ) {
      id
      title
      body
      choices
      start
      end
      snapshot
      state
      author
      space {
        id
        name
      }
    }
  }`,
    variables: {}
  });

  await axios.post('https://testnet.snapshot.org/graphql', data, {
  headers: headers
})
/*.then ((response) => { 
const result =response.data.map(d => ({
author: d.author,
title2: d.title,
active: d.state,
text: d.body,
end: d.end,
start: d.start,
}))
}) */
.then(function (response) {
  console.log(JSON.stringify(response.data));
  setText(response.data);
})
.catch(error => {
  console.log(error);
});
  }
fetchData()
}, []);



return (
    <Container component="main" maxWidth="xs">
    <Button
    fullWidth
    variant="contained"
    sx={{ mt: 3, mb: 2 }}
    onClick={() => CreateProposal()}
  >
    Create
  </Button>
  </Container>
  );
}

This is the response from the console.log: {"data":{"proposals":[{"id":"0xc1d3a777729a2f595be7723b22f406b9bba68d4cf8e5e5a7a81e8f772860a40b","title":"Test proposal using Snapshot.js","body":"hellooooooo","choices":["Alice","Bob","Carol"],"start":1636984800,"end":1637244000,"snapshot":"13620822","state":"closed","author":"0xc1Fe907685762EF263EfF347e28f55C2aC3b7b96","space":{"id":"zischan.eth","name":"ZA Network"}},{"id":"0x39fff4b49a903dbaaaca902b1a87863aef1626fde0c49568eee344a5f0d05970","title":"Test proposal using Snapshot.js","body":"","choices":["Alice","Bob","Carol"],"start":1636984800,"end":1637244000,"snapshot":"13620822","state":"closed","author":"0xc1Fe907685762EF263EfF347e28f55C2aC3b7b96","space":{"id":"zischan.eth","name":"ZA Network"}},{"id":"0xfe91b0d504691b803c40d5ccff130996947d7f218feee5debc689a4f00432c95","title":"Test proposal using Snapshot.js","body":"","choices":["Alice","Bob","Carol"],"start":1636984800,"end":1637244000,"snapshot":"13620822","state":"closed","author":"0xc1Fe907685762EF263EfF347e28f55C2aC3b7b96","space":{"id":"zischan.eth","name":"ZA Network"}},{"id":"0x58a57b27729e231cc8432c235edb2130dc52f8d42e6f0defc44d6699e15b167d","title":"Test proposal using Snapshot.js","body":"","choices":["Alice","Bob","Carol"],"start":1636984800,"end":1637244000,"snapshot":"13620822","state":"closed","author":"0xc1Fe907685762EF263EfF347e28f55C2aC3b7b96","space":{"id":"zischan.eth","name":"ZA Network"}},{"id":"0xdc010283457859254f7f9b31b5e5543e6d2c846f3727ee54303b4c0823783a06","title":"Test proposal using Snapshot.js","body":"","choices":["Alice","Bob","Carol"],"start":1636984800,"end":1637244000,"snapshot":"13620822","state":"closed","author":"0xc1Fe907685762EF263EfF347e28f55C2aC3b7b96","space":{"id":"zischan.eth","name":"ZA Network"}},{"id":"0x2c16905e11ad0c9d0ed6034933844f7295f3bb77e923f9e2bd8fbf445a89e76d","title":"Test proposal using Snapshot.js","body":"","choices":["Alice","Bob","Carol"],"start":1636984800,"end":1637244000,"snapshot":"13620822","state":"closed","author":"0xc1Fe907685762EF263EfF347e28f55C2aC3b7b96","space":{"id":"zischan.eth","name":"ZA Network"}},{"id":"0xa6964d2b008f6dfb7a5fbf42d17b6d74a4d3579b64bfc822b56b2df1964dc57f","title":"Wie gehts dir?","body":"Sag mir wie es dir geht","choices":["gut","schlecht"],"start":1656262678,"end":1656521878,"snapshot":"10920461","state":"active","author":"0xc1Fe907685762EF263EfF347e28f55C2aC3b7b96","space":{"id":"zischan.eth","name":"ZA Network"}}]}}

CodePudding user response:

As far as I and @Jballin can see, you are console.logging an extra "data", with a "response" inside. Could you please try with:

response.data.data.proposals.map()

Please, let us know if it solves your problem!

  • Related