Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

strapi v4 populate

// path:srcapieventcontrollers
/**
 *  event controller
 */

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

// module.exports = createCoreController('api::event.event');

module.exports = createCoreController('api::event.event', ({strapi}) => ({
  //Find with populate ----------------------------------------
  async find(ctx) {
    const populateList = [
      'image',
      'user',
    ]
    // Push any additional query params to the array
    populateList.push(ctx.query.populate)
    ctx.query.populate = populateList.join(',')
    // console.log(ctx.query)
    const content = await super.find(ctx)
    return content
  },

  // Create user event----------------------------------------
  async create(ctx) {
    let entity;
    ctx.request.body.data.user = ctx.state.user;
    entity = await super.create(ctx);
    return entity;
  },
  // Update user event----------------------------------------
  async update(ctx) {
    let entity;
    const {id} = ctx.params;
    const query = {
      filters: {
        id: id,
        user: {id: ctx.state.user.id}
      }
    };
    const events = await this.find({query: query});
    console.log(events);
    if (!events.data || !events.data.length) {
      return ctx.unauthorized(`You can't update this entry`);
    }
    entity = await super.update(ctx)
    return entity;
  },

  // Delete a user event----------------------------------------
  async delete(ctx) {
    const {id} = ctx.params;
    const query = {
      filters: {
        id: id,
        user: {id: ctx.state.user.id}
      }
    }
    const events = await this.find({query: query});
    if (!events.data || !events.data.length) {
      return ctx.unauthorized(`You can't delete this entry`);
    }
    const response = await super.delete(ctx);
    return response;
  },
  // Get logged in users----------------------------------------
  async me(ctx) {
    const user = ctx.state.user;
    if (!user) {
      return ctx.badRequest(null, [
        {messages: [{id: "No authorization header was found"}]},
      ]);
    }
    const query = {
      filters: {
        user: {id: user.id}
      }
    }
    const data = await this.find({query: query});
    if (!data) {
      return ctx.notFound();
    }
    const sanitizedEntity = await this.sanitizeOutput(data, ctx);
    return this.transformResponse(sanitizedEntity);
  },
}));
Comment

populate strapi v4

// path:srcapieventcontrollers
/**
 *  event controller
 */

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

// module.exports = createCoreController('api::event.event');

module.exports = createCoreController('api::event.event', ({strapi}) => ({
  //Find with populate ----------------------------------------
  async find(ctx) {
    const populateList = [
      'image',
      'user',
    ]
    // Push any additional query params to the array
    populateList.push(ctx.query.populate)
    ctx.query.populate = populateList.join(',')
    // console.log(ctx.query)
    const content = await super.find(ctx)
    return content
  },

  // Create user event----------------------------------------
  async create(ctx) {
    let entity;
    ctx.request.body.data.user = ctx.state.user;
    entity = await super.create(ctx);
    return entity;
  },
  // Update user event----------------------------------------
  async update(ctx) {
    let entity;
    const {id} = ctx.params;
    const query = {
      filters: {
        id: id,
        user: {id: ctx.state.user.id}
      }
    };
    const events = await this.find({query: query});
    console.log(events);
    if (!events.data || !events.data.length) {
      return ctx.unauthorized(`You can't update this entry`);
    }
    entity = await super.update(ctx)
    return entity;
  },

  // Delete a user event----------------------------------------
  async delete(ctx) {
    const {id} = ctx.params;
    const query = {
      filters: {
        id: id,
        user: {id: ctx.state.user.id}
      }
    }
    const events = await this.find({query: query});
    if (!events.data || !events.data.length) {
      return ctx.unauthorized(`You can't delete this entry`);
    }
    const response = await super.delete(ctx);
    return response;
  },
  // Get logged in users----------------------------------------
  async me(ctx) {
    const user = ctx.state.user;
    if (!user) {
      return ctx.badRequest(null, [
        {messages: [{id: "No authorization header was found"}]},
      ]);
    }
    const query = {
      filters: {
        user: {id: user.id}
      }
    }
    const data = await this.find({query: query});
    if (!data) {
      return ctx.notFound();
    }
    const sanitizedEntity = await this.sanitizeOutput(data, ctx);
    return this.transformResponse(sanitizedEntity);
  },
}))
Comment

PREVIOUS NEXT
Code Example
Javascript :: elixir guards 
Javascript :: side effect, useEffect, return 
Javascript :: javascript Check Map Elements 
Javascript :: react native image from web 
Javascript :: react particles js 
Javascript :: try catch 
Javascript :: data-toggle="tooltip not working due to jquery-ui.min.js 
Javascript :: js get html title 
Javascript :: draft js insert text example 
Javascript :: email valid javascript 
Javascript :: JS cast a Number into a String 
Javascript :: The reduce() method executes a reducer function on each element of the array and returns a single output value. 
Javascript :: get location 
Javascript :: react spread operator 
Javascript :: node http request 
Javascript :: Selectores de jQuery CSS básicos 
Javascript :: class component in react 
Javascript :: chrome.browseraction.getbadgetext 
Javascript :: node js install aws-sdk 
Javascript :: flutter inject javascript in flutter webview 
Javascript :: map function with params 
Javascript :: how to pass props to another component 
Javascript :: add int to string javascirpt 
Javascript :: js document on load 
Javascript :: what is the slice method in javascript 
Javascript :: javascript pipe function 
Javascript :: export multiple function in node js 
Javascript :: how to append a data to list in redux 
Javascript :: merge two binary tree 
Javascript :: how to redirect to another page without writing javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =