keystonejs / keystone-classic

Node.js CMS and web app framework
http://v4.keystonejs.com
MIT License
14.62k stars 2.21k forks source link

Unsetting or clearing out DateTime field does not actually unset data in field #4965

Open dfinton opened 5 years ago

dfinton commented 5 years ago

Expected behavior

If someone wants to clear out the data for a DateTime field in the UI (i.e. set it to null), they should be able to delete the data in the inputs, click "save", and see that the DateTime is no longer set for that field within that document.

Actual/Current behavior

After clicking "Save" after clearing out the existing DateTime value, it appears that the field was cleared out as expected, but when you refresh the page in the UI, the original DateTime value is still set for that field. Also:

Steps to reproduce the actual/current behavior

  1. Go to admin UI, edit existing document with a DateTime field set. Set that value to any date/time.
  2. Click "Save". Note that the DateTime field is correctly updated. Refreshing the page shows that the date and time are correctly set.
  3. Go to same record, and remove the date and time strings from both form inputs
  4. Click "Save" again.
  5. Field data should be set to "null" in the database and the fields should be empty in the UI even after a refresh
  6. Instead, the UI makes it look like the data was unset, but if you refresh the page, the original DateTime field returns. Also verified this by looking at the database directly and through console.log dumps in a pre-save and pre-validate hook; instead of empty data, the original timestamp is sent to the database to store.

Here's the config for that field in our model:

    {
      label: 'Expire At',
      type: Types.Datetime,
      utc: true,
      format: 'YYYY-MM-DD',
      default: null,
    };

It's worth noting that the default value (null) is honored. Also, according to the mongoose documentation, "null" (the value, not the string) is a valid value for a DateTime record. The moment an actual date/time is saved to that field, however, it can never be unset.

Right now our workaround for this issue is to set the value of our data to some date in the distant future, i.e. sometime in 2030.

Environment

Software Version
Keystone 4.2.1
Node.js 10.15.3 (reproducible irrespective of supported node version)
Browser Chrome/Firefox. Likely affects all browsers