GithubHelp home page GithubHelp logo

Comments (12)

Rendxn avatar Rendxn commented on May 28, 2024 2

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

from strapi.

Rendxn avatar Rendxn commented on May 28, 2024

I would also like to know

from strapi.

NooBiToo avatar NooBiToo commented on May 28, 2024

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

from strapi.

NooBiToo avatar NooBiToo commented on May 28, 2024

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?

from strapi.

benjamincanac avatar benjamincanac commented on May 28, 2024

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

from strapi.

luixal avatar luixal commented on May 28, 2024

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!

from strapi.

benjamincanac avatar benjamincanac commented on May 28, 2024

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

from strapi.

alexey13 avatar alexey13 commented on May 28, 2024

@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.

from strapi.

benjamincanac avatar benjamincanac commented on May 28, 2024

@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?

from strapi.

alexey13 avatar alexey13 commented on May 28, 2024

Yeah, I use qs and it works nice!

from strapi.

luixal avatar luixal commented on May 28, 2024

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!

from strapi.

mrsuarezg avatar mrsuarezg commented on May 28, 2024

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

from strapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.