Cannot update a file field on Before Update Hook

Hi all !
I have a table that contains a file field type. When Im creating a record on that table, I have no issues at all.
This is what I do:

As you can see here, I just:

  • Get field content and upload it on cloudinary
  • Get the id cloudinary is returning to me
  • I save that id on table field

Perfect !

Problem is when I want to update that file field type. I have two issues there:

Problem number ONE

  • When I want to unassign a file to that field ( clicking the ‘X’ here and sending NO file, just like Im showing in the image:

Im getting this error:

Problem number TWO

The opposite situation. I have a record with an empty file and I want to set a file on it. What happens there is this:

Compiler is telling me I cannot modify the content of a readonly type, so, how can I set a file id in there if the field is read only ??

Feature(s) impacted

Its impossible to update a file field using Before Update Hooks

Observed behavior

  • If file field was null and you want to assign a new file content, compiler dont let you assign a id value in that field
  • If file field was NOT null and you want nullify that field, you get a console error on PUT

( Both situations described above )

Expected behavior

  • If I want to delete a file content in a field via edit operation, I should be able to
  • If I want to assign a file content in a field what was unassigned before, I should be able too.

Context

  • Project name: …
  • Team name: Dunsguide
  • Environment name: Development
  • Agent type & version: @forestadmin/agent = 1.0.0-beta.43, @forestadmin/datasource-sql=1.0.0-beta.40

Hey :wave:

Did you consider using replaceFieldWriting (doc) instead of hooks for your context?

Indeed, we provide the context as a readonly variable deliberately, especially to prevent dynamically changing values (We do a lot of validation on all of the object we handle. Dynamically modifying values in a hook could lead to super hard-to-debug issues)

Hooks is a very low level feature, and I really think that the use-case you are sharing would be easier to implement using the replaceFieldWriting. I may have missed something though, so don’t hesitate to share more informations on your use case.

1 Like

Hi, Jeff !!

I implemented what you specify here, and … its working perfectly in the SECOND case ( if a file field is emply and I want to fill it, it works great.

But, if I already saved field file information and I want to ERASE it, I still got this:

1 Like

Good to know, I’ll mark the topic as closed then (Don’t hesitate to open a new one if you were to find any issue with replaceFieldWriting).

On my end, I’ll take a look at the document, to check if we can make this clearer that hooks weren’t design for field update :+1:

@jeffladiray … I sang the Victory song before testing everything… haha !