React, map with 2 json without id

Asked

Viewed 90 times

-3

I would like some help: I have two Json: posts and Author. Json posts have no id. Ojson returns right in the log. I already have the card component created, but I can’t render the posts on the screen. Each post has a respective author. Grateful

Jsons : Publications: http://www.mocky.io/v2/5be5e3fa2f000082000fc3f8 Authors: http://www.mocky.io/v2/5be5e3ae2f00005b000fc3f6

App.js:

import Routes from './routes';
import api from '../src/services/api';

import Navbar from '../src/components/Navbar';
import PostCard from '../src/components/PostCard';

function App() {
  const [posts, setPosts] = useState([]);

  useEffect(() => {
    async function loadPosts() {
      const response = await api.get('5be5e3fa2f000082000fc3f8')
      const { postsApi } = await response.data

      console.log(response.data);

      setPosts(postsApi);
    }
    loadPosts();
  }, []);

  return (
    <>
    <div id="App">
      <Navbar></Navbar>
      <section className="cards">
        {posts?.map((post, index) => (
          <div key={index}>
            <PostCard post={post} ></PostCard>
          </div>
        ))}
      </section>
    </div>
    </>
  );
}

export default App; ```



  • And what’s the problem? A key on the basis of index?

  • Does not render posts on screen..

  • Ah, yes... It is written in the question but passed me beaten. With the syntax posts.map (removing the question mark ?) works? I know this syntax in Kotlin but I’ve never seen in React...

  • Hum.Does not work. Does not recognize the Function map if you take the ?

  • If you put a console.log(posts) before the return, what appears on the console? Edith the question with that information.

  • Returns right. As in link

  • If the variable posts is with the correct value (containing an array), posts.map must work. If you put, inside the map, tags only p, for example <p>Teste</p>, he still shows nothing?

  • Still continues.

  • your doubt is how to unite these two results?

  • Yes, unite the two

  • So change the question and ask how I make the union that by what I can understand you can already rescue the two json.

  • It was giving error, but it was fixed.. But the second step would be to join the two JSON, what is still missing..

Show 7 more comments

1 answer

0


Analyzing your code better I realized that the error is on the line const { postsApi } = await response.data

Problems:

console.log

You say that the console.log returns the correct JSON, but you call console.log(response.data) and not console.log(postsApi), which is where you are storing the desired data. Consequently, you don’t know if the variable postsApi is with the correct data.

await response.data

That doesn’t make sense. You used await in the api.get, which is asynchronous, does not need to use await to read an object attribute.

const { postsApi } = await response.data

Finally (and the root of your question), by removing the await, we have const { postsApi } = response.data. That’s the same as const postsApi = response.data.postsApi, Can you see the error now? The object response.data does not have an attribute postsApi.

Solution

As stated in the previous paragraph, you just need to fix how to store the data obtained from the api. If you don’t understand how your previous code works, read on Allocation via de-structuring.

const postsApi = response.data
  • Puts!!! I don’t believe it. kk True.. But now to catch the author of the other Json?

  • Do a procedure similar to what you did to get the posts and store them in the way that makes sense in your application. For example, after picking up the postsApi, you get the authors (with a request) and make a mapping to associate the posts with the correct authors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.