Skip to content

Commit

Permalink
fix: assign table column discrim before setting field discrim
Browse files Browse the repository at this point in the history
Mongoose now saves objects with keys in the order the keys are specified in the schema, meaning the table column discriminator would not have been applied at the time the table field was assigned as a discriminator to the form's form fields.

This resulted in a bug where table columns did not have a discriminated schema causing uncaught errors to be thrown when a table field was submitted, such as

"UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'selectedValidation' of undefined" as the field did not contain the necessary discriminated column schema props due to not being assigned prior to assigning the TableFieldSchema discriminator.

See https://mongoosejs.com/docs/migrating_to_6.html#schema-defined-document-key-order
  • Loading branch information
karrui committed Dec 9, 2021
1 parent 196e391 commit 10b1c4e
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/app/models/form.server.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ const compileFormModel = (db: Mongoose): IFormModel => {
) as Schema.Types.DocumentArray

const TableFieldSchema = createTableFieldSchema()
const TableColumnPath = TableFieldSchema.path(
'columns',
) as Schema.Types.DocumentArray
TableColumnPath.discriminator(
BasicField.ShortText,
createShortTextFieldSchema(),
)
TableColumnPath.discriminator(
BasicField.Dropdown,
createDropdownFieldSchema(),
)

FormFieldPath.discriminator(BasicField.Email, createEmailFieldSchema())
FormFieldPath.discriminator(BasicField.Rating, createRatingFieldSchema())
Expand Down Expand Up @@ -444,17 +455,6 @@ const compileFormModel = (db: Mongoose): IFormModel => {
)
FormFieldPath.discriminator(BasicField.Section, createSectionFieldSchema())
FormFieldPath.discriminator(BasicField.Table, TableFieldSchema)
const TableColumnPath = TableFieldSchema.path(
'columns',
) as Schema.Types.DocumentArray
TableColumnPath.discriminator(
BasicField.ShortText,
createShortTextFieldSchema(),
)
TableColumnPath.discriminator(
BasicField.Dropdown,
createDropdownFieldSchema(),
)

// Discriminator defines all possible values of startPage.logo
const StartPageLogoPath = FormSchema.path(
Expand Down

0 comments on commit 10b1c4e

Please sign in to comment.