A
A
Andrew2018-11-25 01:05:49
Vue.js
Andrew, 2018-11-25 01:05:49

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})
})

The server, according to the method I wrote, gives the correct answer (to my surprise), when accessing the address server/posts/Category name
However, I have no idea how to call this method in vue, or rather, how to pass the desired parameter without switching to another url. I wrote a simple method that would be called on click, but, obviously, the parameter is not passed. I can't figure out what I'm doing wrong.
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 question

Ask a Question

731 491 924 answers to any question