question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

defaultValue function values possibly broken

See original GitHub issue

Bug report

Describe the bug

Ok, so the docs say fields can take a defaultValue key as a string or function. Strings work fine, but I can’t seem to get functions to do anything at all.

I notice they aren’t passed to the Admin UI, so it makes sense they wouldn’t resolve and display when creating an item, but even if you simply save a field with no value the defaultValue function is never called.

Testing using this config in the TODO demo:

keystone.createList('Todo', {
  schemaDoc: 'A list of things which need to be done',
  fields: {
    name: {
      type: Text,
      schemaDoc: 'This is the thing you need to do',
      isRequired: true,
      defaultValue: () => { return 'test text' },
    },
  },
});

List._resolveDefaults is where I would expect the default function to be resolved server-side, but getDefaultValue is only called for id since name has a null value already.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
famelcommented, Jan 11, 2021

Thanx - it seems so. It also seems that it works “on-the-fly” if it is set isRequired:false during creation (without displayed on the creation form)

1reaction
reubanocommented, Feb 23, 2020

Not sure why this isn’t considered a bug. So should the docs be updated to say that defaultValue can only be a scalar?

Read more comments on GitHub >

github_iconTop Results From Across the Web

The defaultValue of <select> may be broken #11099 - GitHub
I would like to report a bug. In re-filing another outstanding bug that was closed despite being reproducible, I decided to create a...
Read more >
Default Value in a C# Function - Stack Overflow
C++ can have default values for parameters... these parameters can be "skipped" when calling a method/function BUT the rule in C++ is that...
Read more >
Be careful when using || to set default values in JavaScript
More importantly, using || to set default values breaks down when falsy values, like 0 or “”, are passed into the function. When...
Read more >
Default parameters - JavaScript - MDN Web Docs
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Read more >
DefaultValue not working | Optimizely Developer Community
I added a new property and it won't set the default value. Am I missing something? This is the new property within the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found