Answer the question
In order to leave comments, you need to log in
How to sort posts in vue?
There is a component that displays all posts from the database.
Here are the main methods currently in use:
//Метод получения постов
fetchPosts () {
return Api().get('posts')
}
//Вызов метода в компоненте
async getPosts () {
const response = await PostsService.fetchPosts()
this.posts = response.data.posts
}
//Запрос к бд на сервере
app.get('/posts', (req, res) => {
Post.find({}, 'title description content image category', function (error, posts) {
if (error) { console.error(error); }
res.send({
posts
})
}).sort({_id:-1})
})
//И метод получения поста по категории на сервере
app.get('/posts/:category', (req, res) => {
Post.find({ 'category': req.params.category }, 'title description content image category', function (error, posts) {
if (error) { console.error(error); }
res.send({
posts
})
}).sort({_id:-1})
})
getPostsByCategory () {
const response = PostsService.fetchPostsByCat({
category: 'Категория'
})
this.posts = response.data.posts32
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question