Dealing with NULL values

I tried to connect a default Redwood app to an existing Postrgresql db. The tables in the db has some data and constraints. I added the one of the table to the Prisma model and then ran the “yarn rw g scaffold Source” (Source is the model name).
It generated the pages and components as needed (Awesome!!!) , but when tried to get to the new generated route ‘/sources’, I got a FatalErrorPage (Something went wrong).
At first I thought I was doing something wrong with the model definition, as the error messages that I received were not getting me to the right place.
Digging more in the error and the definition of the table in the db, led me to the understanding that the list page could not handle NULL values that were returned from the DB.

One of the const in the components is: truncate, that truncate the length of the returned value to the defined max 150 characters.
This definition of the const doesn’t handle correctly NULL values. It need to be corrected.
After I added the correction, the page rendered correctly and with no issues.

The const before the correction (as created by the scaffold action):

const truncate = (text) => {
  let output = text
  if (text.length > MAX_STRING_LENGTH) {
    output = output.substring(0, MAX_STRING_LENGTH) + '...'
  }
  return output
}

The corrected script should be - adding ‘text && ’ to ‘text.length

const truncate = (text) => {
  let output = text
  if (text && text.length > MAX_STRING_LENGTH) {
    output = output.substring(0, MAX_STRING_LENGTH) + '...'
  }
  return output
}

The addition will check if the returned value is not NULL and only then will continue to truncate based on its length. Otherwise an error will be rendered.

Add that change to one of the next releases, if needed.

Hi @wiezmankimchi Thanks for taking Redwood for a spin and for taking the time to let us know about this. And looping in @rob who’s our resident master of all things generators.

I’ve created a corresponding GitHub Issue here with links to the source code and test fixture that should be updated. No pressure to work on this @wiezmankimchi, but we would welcome a PR fix from you if you’d like. Just go for it if so!

Great find @wiezmankimchi, as @thedavid said we’d happily accept a PR that fixes this!

1 Like