@@ -703,7 +703,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
703703 const targetResourcePkFields = { } ;
704704 const pksUniques = { } ;
705705 col . foreignResource . polymorphicResources . forEach ( ( pr ) => {
706- targetResources [ pr . whenValue ] = this . adminforth . config . resources . find ( ( res ) => res . resourceId == pr . resourceId ) ;
706+ if ( pr . resourceId === null ) {
707+ return ;
708+ }
709+ const targetResource = this . adminforth . config . resources . find ( ( res ) => res . resourceId == pr . resourceId ) ;
710+ if ( ! targetResource ) {
711+ return ;
712+ }
713+ targetResources [ pr . whenValue ] = targetResource ;
707714 targetConnectors [ pr . whenValue ] = this . adminforth . connectors [ targetResources [ pr . whenValue ] . dataSource ] ;
708715 targetResourcePkFields [ pr . whenValue ] = targetResources [ pr . whenValue ] . columns . find ( ( col ) => col . primaryKey ) . name ;
709716 const pksUnique = [ ...new Set ( data . data . filter ( ( item ) => item [ col . foreignResource . polymorphicOn ] === pr . whenValue ) . map ( ( item ) => item [ col . name ] ) ) ] ;
@@ -828,7 +835,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
828835 return { error : `Column '${ column } ' in resource '${ resourceId } ' is not a foreign key` } ;
829836 }
830837
831- const targetResourceIds = columnConfig . foreignResource . resourceId ? [ columnConfig . foreignResource . resourceId ] : columnConfig . foreignResource . polymorphicResources . map ( ( pr ) => pr . resourceId ) ;
838+ const targetResourceIds = columnConfig . foreignResource . resourceId ? [ columnConfig . foreignResource . resourceId ] : columnConfig . foreignResource . polymorphicResources . filter ( pr => pr . resourceId !== null ) . map ( ( pr ) => pr . resourceId ) ;
832839 const targetResources = targetResourceIds . map ( ( trId ) => this . adminforth . config . resources . find ( ( res ) => res . resourceId == trId ) ) ;
833840
834841 const responses = ( await Promise . all (
@@ -983,7 +990,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
983990 const targetConnectors = { } ;
984991 const targetResourcePkFields = { } ;
985992 column . foreignResource . polymorphicResources . forEach ( ( pr ) => {
986- targetResources [ pr . whenValue ] = this . adminforth . config . resources . find ( ( res ) => res . resourceId == pr . resourceId ) ;
993+ if ( pr . resourceId === null ) {
994+ return ;
995+ }
996+ const targetResource = this . adminforth . config . resources . find ( ( res ) => res . resourceId == pr . resourceId ) ;
997+ if ( ! targetResource ) {
998+ return ;
999+ }
1000+ targetResources [ pr . whenValue ] = targetResource ;
9871001 targetConnectors [ pr . whenValue ] = this . adminforth . connectors [ targetResources [ pr . whenValue ] . dataSource ] ;
9881002 targetResourcePkFields [ pr . whenValue ] = targetResources [ pr . whenValue ] . columns . find ( ( col ) => col . primaryKey ) . name ;
9891003 } ) ;
@@ -1064,14 +1078,21 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
10641078
10651079 // for polymorphic foreign resources, we need to find out the value for polymorphicOn column
10661080 for ( const column of resource . columns ) {
1067- if ( column . foreignResource ?. polymorphicOn ) {
1081+ if ( column . foreignResource ?. polymorphicOn && record [ column . name ] !== undefined ) {
10681082 let newPolymorphicOnValue = null ;
10691083 if ( record [ column . name ] ) {
10701084 const targetResources = { } ;
10711085 const targetConnectors = { } ;
10721086 const targetResourcePkFields = { } ;
10731087 column . foreignResource . polymorphicResources . forEach ( ( pr ) => {
1074- targetResources [ pr . whenValue ] = this . adminforth . config . resources . find ( ( res ) => res . resourceId == pr . resourceId ) ;
1088+ if ( pr . resourceId === null ) {
1089+ return ;
1090+ }
1091+ const targetResource = this . adminforth . config . resources . find ( ( res ) => res . resourceId == pr . resourceId ) ;
1092+ if ( ! targetResource ) {
1093+ return ;
1094+ }
1095+ targetResources [ pr . whenValue ] = targetResource ;
10751096 targetConnectors [ pr . whenValue ] = this . adminforth . connectors [ targetResources [ pr . whenValue ] . dataSource ] ;
10761097 targetResourcePkFields [ pr . whenValue ] = targetResources [ pr . whenValue ] . columns . find ( ( col ) => col . primaryKey ) . name ;
10771098 } ) ;
0 commit comments