How to get data (collection) related to the logged in user in Strapi 14

51 Views Asked by At

I'm quite new with strapi , and am working on a simple project where I have an api to fech history of purchases http://localhost:1337/api/purchases?populate=* and i updated my strapi controller as the following , however always am getting empty object {}



 const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::purchase.purchase', ({ strapi }) => ({
async create(ctx) {
    try {
        const user = ctx.state.user;
        ctx.request.body.data.users_permissions_user = user.id
        const datas = await strapi.entityService.create("api::purchase.purchase", {
            ...ctx.request.body
        })
        return datas;
    } catch (err) {
        ctx.body = err;
    }
},
 
async find(ctx) {
    try {
        const user = ctx.state.user;
        const data = await strapi.entityService.findMany("api::purchase.purchase", {
            filters: {
                users_permissions_user: {
                    id: user.id
                }
            }
        })
        return data;
    } catch (err) {
        ctx.body = err;
    }
},
})); ```

and the api is returning ok 200

and this is my fetcher in nextjs side:
``` export async function getServerSideProps({ req,locale }) {
  const { token } = cookie.parse(req.headers.cookie);
   
  
  const purchasesResponse = await fetcher(
    `http://localhost:1337/api/purchases?populate=*`,{
      headers: {
        Authorization: `Bearer ${token}`,
      },
    }
  );
   console.log('data purchases '+ purchasesResponse)
 
    return {
 
      props: {
        ...(await serverSideTranslations(locale, ['common','loyalty'])),
        purchases: purchasesResponse,
      },
    };
  
}```
0

There are 0 best solutions below