"Unexpected error" when running smart action in preexisting NodeJs / Sequelize App

Expected behavior

Created a my_collection.js file in a /forest dir in project root with the following content:

const { collection } = require("forest-express-sequelize");

collection("FormTemplate", {
  actions: [
    {
      name: "Make Active",
    },
  ],
});

And the action appears in the actions menu:

I also created a route handler:

const { FormTemplate } = require("../models");
const express = require("express");
const router = express.Router();
const { PermissionMiddlewareCreator } = require("forest-express-sequelize");
const permissionMiddlewareCreator = new PermissionMiddlewareCreator(
  "FormTemplate"
);

router.post(
  "/forest/actions/make-active",
  permissionMiddlewareCreator.smartAction(),
  (req, res) => {
    console.log("Hello world!");
    return new RecordsGetter(FormTemplate).getIdsFromRequest(req).then((id) => {
      return FormTemplate.update({ active: true }, { where: { id: id } }).then(
        () => {
          res.send({ success: "Company is now live!" });
        }
      );
    });
  }
);

My cors settings are the following:

app.use("^(?!forest/?$).*", cors());

Actual behavior

I get the following error:

Failure Logs

Looking at the network tab of the dev console:

I can hit the /forest/actions/make-active route from Postman, but get the following error, which makes sense since there is no payload:

/home/camille/code/foxxbee/fa-demo-sequelize/node_modules/forest-express/dist/middlewares/permissions.js:31
  return user.renderingId;
              ^

TypeError: Cannot read property 'renderingId' of undefined

Context

Please provide any relevant information about your setup.

  • Package Version:
  • Express Version: 4.17.1
  • Sequelize Version: ^6.6.2
  • Database Dialect: postgresql
  • Database Version: 13
  • Project Name: fa-demo-sequelize

Hello @cooki,

I am investigating your issue, please hold on.

Hi again @cooki,

Can you please test with /actions/make-active instead of /forest/actions/make-active in your route definition? I am thinking that the forest subpath is already defined in an upper scope.

Regards

Hey @Sliman_Medini, sorry for the late reply.

After trying what you suggested, I encountered the following error in the chrome dev console:

Access to fetch at 'http://localhost:3000/forest/actions/make-active' from origin 'https://app.forestadmin.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

Which is weird because I have the following cors config, app.use("^(?!forest/?$).*", cors());, and all other requests coming from the frontend are running smoothly.

Thanks for your time,
Camille

When setting the route path to /forest/actions/make-active, I get the following error:

Access to fetch at 'http://localhost:3000/forest/actions/make-active' from origin 'https://app.forestadmin.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Which suggests the enabling of app.options('/forest/actions/make-active', cors()) , but get the same result. (Taken from the cors package docs)

Found the solution by following a fresh FA setup, and adding the following code to my app:

  app.use(
    "/forest/authentication",
    cors({
      ...corsConfig,
      origin: corsConfig.origin.concat("null"),
    })
  );
  app.use(cors(corsConfig));

  app.use("/forest", (request, response, next) => {
    if (PUBLIC_ROUTES.includes(request.url)) {
      return next();
    }
    return ensureAuthenticated(request, response, next);
  });

The above code must be added before routes are added to the app.

Maybe some docs should be added for developers integrating FA to their already existing Node App.