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

**URL:** <https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878>\
**Category:** Help me!\
**Created:** [July 12, 2021, 9:36am UTC](https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878 "2021-07-12T09:36:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 12, 2021, 9:36am UTC](https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878/1 "2021-07-12T09:36:02Z")

</div>

## Expected behavior

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

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

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

```

And the action appears in the actions menu:

 ![Screenshot 2021-07-12 112545](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/c/c1e7c97d827a9de03b265507a12a161cf9f32cbd.png)

I also created a route handler:

```javascript
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:

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

```

## Actual behavior

I get the following error:

 ![Screenshot 2021-07-12 112340](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/a/a7bcab767641116a639970fcc3748131f702c769.png)

## Failure Logs

Looking at the network tab of the dev console:

 ![Screenshot 2021-07-12 113259](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/7/7e8f2489c85398b77f3e6e44fe8978a478debc5b.png)

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

```javascript
/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

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [July 12, 2021, 1:12pm UTC](https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878/2 "2021-07-12T13:12:00Z")

</div>

Hello @cooki,

I am investigating your issue, please hold on.

---

<div class="post-metadata">

**Author:** ![Sliman\_Medini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sliman_medini/32/19_2.png) [@Sliman\_Medini](https://community.forestadmin.com/u/Sliman_Medini)\
**Post date:** [July 12, 2021, 1:25pm UTC](https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878/3 "2021-07-12T13:25:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 14, 2021, 7:26am UTC](https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878/4 "2021-07-14T07:26:19Z")

</div>

Hey @Sliman_Medini, sorry for the late reply.

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

```javascript
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

---

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 14, 2021, 8:38am UTC](https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878/5 "2021-07-14T08:38:02Z")

</div>

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

```javascript
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](https://www.npmjs.com/package/cors#enabling-cors-pre-flight))

---

<div class="post-metadata">

**Author:** ![cooki](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cooki/32/2657_2.png) [@cooki](https://community.forestadmin.com/u/cooki)\
**Post date:** [July 14, 2021, 10:01am UTC](https://community.forestadmin.com/t/unexpected-error-when-running-smart-action-in-preexisting-nodejs-sequelize-app/2878/6 "2021-07-14T10:01:52Z")

</div>

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

```javascript
  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.
