Skip to content

lifecycle hooks not working in Collections with draft/publish-System #8329

@pointout

Description

@pointout

Describe the bug
I am using lifecycle hooks in models to update fields and create the content for my own slug fields. This works, if the draft-System is off. But when I activate the draft system, data.fieldname is undefined.

Steps to reproduce the behavior

  1. Turn off the draft-system for a collection
  2. Change data and save them
  3. The lifecacle hooks beforeUpdate and beforeCreate are working as expected
  4. Turn on the draft-system for the collection
  5. unpublish or edtit fields
  6. The field are not changed correctly by lifecycle hook, but getting undefined.

Code snippets
/api/product/models/product.js

'use strict';
const slugify = require('slugify');

const mySlugify = (slug) => { 
  return (
    slugify(slug, { 
      remove:/[*+~.(){}\[\]\\/'"!:@,]/g, 
      lower:true 
    })
  );
};

/**
 * Lifecycle callbacks for the `product` model.
 */

module.exports = {

  lifecycles: {
    async beforeCreate(data) {
 
      if (!data.name_en) {
        data.name_en = data.name_de;
      }
      data.slug_de = mySlugify(data.name_de + '-' + data.orderno);
      data.slug_en = mySlugify(data.name_en + '-' + data.orderno);

    },

    async beforeUpdate (params,data) {
      console.log(data);

      if (!data.name_en) {
        data.name_en = data.name_de;
      }
      data.slug_de = mySlugify(data.name_de + '-' + data.orderno);
      data.slug_en = mySlugify(data.name_en + '-' + data.orderno);
      
    }
  }
};

without draft-system:
without_draftsystem

with daft-system:
with-draft-system

In another collection doing the same, data.name_de is logged as undefined, but there is a string in that field.

System

  • Node.js version: v12.18.4
  • NPM version: 6.14.6
  • Strapi version: 3.2.3
  • Database: sqlite
  • Operating system: Windows 10 Pro

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions