Seajanjan
Seajanjan

Reputation: 219

How to query collections from custom plugin in strapi?

I want to collect data from my collections and display it in my own plugin, for example 'Cars'. I have not found anything about this and do not know how to approach this.

import React, { memo } from 'react';
import pluginId from '../../pluginId';
 
const HomePage = () => {
  const fetchData = () => {
      // Here I want to fetch data from my collection and display it
      return null;
  }
 
  return (
    <div>
      <h1>{pluginId}&apos;s HomePage</h1>
      <p>Happy coding</p>
      {fetchData()}
    </div>
  );
};
 
export default memo(HomePage);

Upvotes: 4

Views: 1849

Answers (2)

jerome laval
jerome laval

Reputation: 54

Case : Api model :

const cars = await strapi.query('car').find({});

Case : Plugin model :

const cars = await strapi.query('car', 'plugin_name').findOne({});
// or
const cars = await strapi.query('car', 'plugin_name').findMany({});

Upvotes: 0

Martin Gaviola
Martin Gaviola

Reputation: 21

Old question but I've been looking for the answer and it's difficult to find. So the solution for this, is to use the endpoints provided by the content-manager plugin of strapi.

First you should go and allow public access to this endpoints in Settings then Roles & Permissions plugin.

Finally you can query your data like this

const response = await request("/content-manager/collection-types/application::cars.cars", {
        method: "GET"
      });
}

Upvotes: 2

Related Questions