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 :: Register Multiple Models In Admin 
Javascript :: getters javascript 
Javascript :: Naming Your Componts Vue 
Javascript :: JS function examples 
Javascript :: javascript Program for sum of arithmetic series using loop 
Javascript :: Constructing a URL from component parts and getting the constructed string 
Javascript :: javascript get object list by value 
Javascript :: javascript enum includes value 
Javascript :: react js public folder image path search 
Javascript :: Is It Negative Zero (-0)? js 
Javascript :: prisma.db json 
Javascript :: ajax fail function parameters 
Javascript :: advanced javascript concepts 
Javascript :: capitalize last letter javascript 
Javascript :: click page object 
Javascript :: shell curl path of json file as parameter without temporal file 
Javascript :: javascript function counting cards 
Javascript :: multply js 
Javascript :: mongodb-nodejs-driver-deprecationwarning-collection-count-is-deprecated 
Javascript :: how to create a new window with a specifc link jquery 
Javascript :: The complete map() method syntax 
Javascript :: mongoose.js clause where 
Javascript :: negative index javascript 
Javascript :: -1 in js 
Javascript :: Delete Button not working with json server using angularjs 
Javascript :: Porting Promise.all functionality from AngularJs to VueJs 
Javascript :: want the app to save the passing screen after a user has passed the test even when the app exits in react native 
Javascript :: coin gecko api 
Javascript :: socket.io authentication 
Javascript :: Alternative Bind() Syntax For JavaScript 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =