nuxt-modules / strapi

Strapi Module for Nuxt
https://strapi.nuxtjs.org
MIT License
634 stars 80 forks source link

Parameters documentation #66

Closed DragonWhisperer closed 3 years ago

DragonWhisperer commented 4 years ago

The find(entity, params) only shows the basics. How to use all the parameters here? Can you provide more examples?

Rendxn commented 4 years ago

I would also like to know

NooBiToo commented 4 years ago

Я тоже хотел бы знать

NooBiToo commented 4 years ago

hi guys, @DragonWhisperer , @Rendxn i'm used _sort param, ex: articles: await $strapi.find("articles", { _sort: "created_at:desc" }) I think the other parameters are used the same way but I would also like to sort the articles into categories,

async fetch() {
    this.categories = await this.$strapi.find("categories", {
      _sort: "articles.created_at:desc",
    });
  },

but that doesn't work, can you give me a hint?

benjamincanac commented 3 years ago

The Strapi module is a just a wrapper to call your API. Are you sure GET /categories?_sort=articles.created_at:desc works?

Rendxn commented 3 years ago

Actually, it works fine using the $strapi.find('categories', { _sort: 'created_at:desc }) and the same applies for any other Strapi parameter (_limit, _sort, _start, etc) If you wanna use categories, you should take a look at the Strapi guide, where they use Restaurants and Categories, then when you fetch your articles you could do: $strapi.find('articles', { category.name: yourCategoryName }). Also, if you are doing $strapi.find('categories', { _sort: 'created_at:desc }), this will get the articles related to each category with them unless you set them to private in your model json.

Edit: If you wanna get all the articles for a given set of categories, you need to use the 1st Method shown here, in which they pass an array $strapi.find('products', [['categories.name', 'women'], ['categories.name', 'men']]). Or if you want to get the products that have both men and women as their category, you need to do the 2nd Method shown there await $strapi.find('products', { 'categories.name': ['women', 'men'] })

luixal commented 3 years ago

I'm having a problem here when trying to use _where. As per the docs here, this should work:

const query = qs.stringify({ _where: { _or: [{ stars: 1 }, { pricing_gt: 30 }] } });
await request(`/restaurant?${query}`);

I've tried this:

params._where = {
  _or: [
    {name_contains: 'a'},
    {description_contains: 'a'},
    {serial_contains: 'a'}
  ]
}

and this:

params._where = qs.stringify({
  _or: [
    {name_contains: 'a'},
    {description_contains: 'a'},
    {serial_contains: 'a'}
  ]
})

with no luck. I get a 404 Bad Request error with the message: HTTPError: Your filters contain a field '0' that doesn't appear on your model definition nor it's relations. If I built the query manually, like this:

params['_where[_or][0][name_contains]'] = 'a';
params['_where[_or][1][description_contains]'] = 'a';
params['_where[_or][2][serial_contains]'] = 'a';

It works. I guess there's some problem when building the url with complex params. Maybe the use of qs.stringify for the whole params object is needed, don't know.

Anyone has been lucky doing something like this?

Thanks!

benjamincanac commented 3 years ago

@luixal You don't need to use qs.stringify here, the module does it for you, check out the documentation here.

$strapi.find('restaurants', { _where: { _or: [{ stars: 1 }, { pricing_gt: 30 }] } })
alexey13 commented 3 years ago

@benjamincanac On my end it not works. Can you please test it?

$strapi.find('restaurants', { _where: { _or: [{ stars: 1 }, { pricing_gt: 30 }] } })
error http://localhost:1337/restaurants?_where=%5Bobject+Object%5D

I think that nested objects not allowed.

benjamincanac commented 3 years ago

@luixal @alexey13 Indeed my bad, it seems that ky doesn't support complex objects.

You have to use the qs library I guess, is this the one you tried?

alexey13 commented 3 years ago

Yeah, I use qs and it works nice!

luixal commented 3 years ago

I didn't get it working back in the day, anyway, we had to go other way that fit better our needs. I cannot test it right now but seeing @alexey13 confirmation I'll take it into consideration for next time :)

Thanks!

mrsuarezg commented 2 years ago

Por si ha alguien le sirve yo utilize qs, pero cometía el error de pasarlo: const academy = await this.$strapi.find('collection-example', { queryCollection }) Cuando la forma correcta era usar el cliente http de Strapi puesto que el módulo de Nuxt no soporta nativamente la versión 4 aunque lo diga. Lo siguiente funcionó para mi: const results = await this.$strapi.$http.$get('collection-example', { searchParams: queryCollection })