Skip to content

Instantly share code, notes, and snippets.

@chamatt
Last active November 30, 2018 23:23
Show Gist options
  • Select an option

  • Save chamatt/bf29d00cf4cbe78a8b343cecbebbf6f2 to your computer and use it in GitHub Desktop.

Select an option

Save chamatt/bf29d00cf4cbe78a8b343cecbebbf6f2 to your computer and use it in GitHub Desktop.
Obter Posts
function ObterPosts() {
const secaoblog = document.querySelector("section.blog-container"); // Seleciona o elemento do DOM
axios.get("http://localhost:3000/posts").then(resposta => {
// Faz uma Array com os posts no formato do template
const ListaDePosts = resposta.data.map(post => PostTemplate(post.title, post.body, post.image, post.id));
let PostsJuntos = ListaDePosts.reverse().join(); // Inverte a lista e junta todos os posts em uma string só
if (ListaDePosts.length == 0) // Checamos se existe pelo menos um post no banco de dados, senão, mostramos uma mensagem.
PostsJuntos = `<div class="alert alert-info" role="alert"> Nenhum post! Crie um novo! </div>`;
secaoblog.innerHTML = PostsJuntos; // Insere a string dentro da seção dos posts
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment