ForestAdmin / forest-express-sequelize

🌱 Express/Sequelize agent for Forest Admin to integrate directly to your existing Express/Sequelize backend application.
https://www.forestadmin.com
GNU General Public License v3.0
190 stars 48 forks source link

Many to many throwing an error #380

Closed elie222 closed 4 years ago

elie222 commented 4 years ago

In my student model I have:

  Student.associate = (models) => {
    Student.belongsToMany(models.cohort, {
      through: models.student_cohorts_cohort
    })
  };

In my cohort model I have:

  Cohort.associate = (models) => {
    Cohort.belongsTo(models.program, {
      foreignKey: {
        name: 'programId',
      },
      as: 'program',
    });

    Cohort.belongsToMany(models.student, {
      through: models.student_cohorts_cohort
    })
  };

Viewing students in the cohort views works fine. Viewing cohorts in the student view throws an error. Specifically count endpoint has an issue because association is undefined here:

function HasManyGetter(model, association, opts, params) {
  var queryBuilder = new QueryBuilder(model, opts, params);
  var schema = Interface.Schemas.schemas[association.name];

Expected behavior

Should load many to many relation data.

Actual behavior

Forest Admin shows the following error message:

Oops, something went wrong An error occured while retrieving your data. Please contact support@forestadmin.com for further investigation.

Failure Logs

spark-forest-admin | GET /forest/student/a9b74dd0-507e-48d3-83e3-7910d3e9c394/relationships/cohorts?page%5Bnumber%5D=1&page%5Bsize%5D=15&timezone=Asia%2FJerusalem&sort=-id&fields%5Bcohort%5D=id%2Cname%2CendDate%2Cprogram%2CstartDate 500 79 - 7.040 ms
spark-forest-admin | [forest] 🌳🌳🌳  Unexpected error: Cannot read property 'name' of undefined
spark-forest-admin | TypeError: Cannot read property 'name' of undefined
spark-forest-admin |     at new HasManyGetter (/usr/src/app/node_modules/forest-express-sequelize/dist/services/has-many-getter.js:19:64)
spark-forest-admin |     at count (/usr/src/app/node_modules/forest-express/dist/routes/associations.js:95:12)
spark-forest-admin |     at Layer.handle [as handle_request] (/usr/src/app/node_modules/forest-express/node_modules/express/lib/router/layer.js:95:5)
spark-forest-admin |     at next (/usr/src/app/node_modules/forest-express/node_modules/express/lib/router/route.js:137:13)
spark-forest-admin |     at dispatch (/usr/src/app/node_modules/compose-middleware/lib/index.js:34:24)
spark-forest-admin |     at next (/usr/src/app/node_modules/compose-middleware/lib/index.js:41:24)
spark-forest-admin |     at ipAuthorizer (/usr/src/app/node_modules/forest-express/dist/middlewares/ip-whitelist.js:23:12)
spark-forest-admin |     at /usr/src/app/node_modules/forest-express/dist/services/auth.js:61:10
spark-forest-admin |     at dispatch (/usr/src/app/node_modules/compose-middleware/lib/index.js:53:32)

Context

elie222 commented 4 years ago

Managed to find the issue 😂

I had added this to my code during experimentation:

  fields: [
    {
      field: "cohorts",
      type: "[String]",
      get: student => {
        return student.firstName + " " + student.lastName;
      }
    }
  ],

The cohorts field must have conflicted.