Skip to content

querying component attribute in afterSave does not return the updated value #5185

@speexy

Description

@speexy

Describe the bug
In the afterSave method of my model/content-type, I want to query an attribute of a component that is related to the model/content-type being saved.
example: I have a page (model) with a SEO-component. I update a SEO field and save the page. when I query the SEO field in the afterSave method of the page, I do not get the updated value of the SEO field.

Steps to reproduce the behavior
create a component "SEO" with a field "seo_title":
use this component in a content-type and add a seo_title like "title1_edit0"
fill out and save. check your database for the id of this seo-entry.

create the afterSave-method, find the seo_title you want to check by adding <seo_title_ID> that you looked up in your db.

async(model, response, options) =>{
   await strapi.components['seo.seo'].query(qb=> {
      qb.where("id", "=", "<seo_title_ID>")
    }).fetch().then(r =>console.log("not updated!", r.attributes, "not changed!", r.hasChanged))

now update the seo_title to "title1_edit1" and hit save to trigger the above function.

Expected behavior
I expect the console output to be "title1_edit1", however I get "title1_edit0".
Also I think hasChanged should return TRUE, but it returns FALSE
When I query my database directly:
SELECT seo_title FROM seo WHERE id=<seo_title_ID>
I get "title1_edit1"

**Why do I want to do this anyway"
I want to create slugs from seo_titles. Slugs have a many-to-one relationship with pages. When the seo_title get's updated an new slug for that page should be created.

System

  • Node.js version: ">=10.0.0",
  • NPM version: ">=6.0.0"
  • Strapi version: "3.0.0-beta.18.2",
  • Database: postgres 12.1 (Debian 12.1-1.pgdg100+1)
  • Operating system: docker/ubuntu16.04LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions