Unable to use implement smart actions

Ok, the code below should include the minimal requirements.

const env = process.env.NODE_ENV;
let debug = null;

if (env !== 'production') {
  const non_prod_config = require('dotenv').config();
  debug = require('debug')('index');
  debug(`app started in ${process.env.NODE_ENV} environment`);
  if (non_prod_config.error) {
    throw non_prod_config.error;
  }
} else {
  debug = require('debug')('index');
}
//const app = require('./app')
const express = require('express');
const app = express();
const routes = require('./routes/index');

process.on('unhandledRejection', (reason, p) => {
  debug('Unhandled Rejection at:', p, 'reason:', reason);

  process.exit(1);
  // Application specific logging, throwing an error, or other logic here
});

process.on('uncaughtException', err => {
  debug('Uncaught Exception');
  debug(err);

  process.exit(1);
});
app.use('/', routes);
const server = app.listen(8000, async () => {
  debug('server is runnning on port 8000');

  const forest = require('forest-express-sequelize');
  const db = require('./db/index');

  app.use(
    await forest.init({
      envSecret: process.env.FOREST_ENV_SECRET,
      authSecret: process.env.FOREST_AUTH_SECRET,
      objectMapping: db.Sequelize,
      connections: { default: db.sequelize }
    })
  );
});

Let me know what the error is. We might speak about CORS then.

Also, did you make sure the environment variables below are properly set? I think yes as you said editing records worked.

FOREST_ENV_SECRET=****
FOREST_AUTH_SECRET=****
APPLICATION_URL=****

Yes the variables are set properly but just to be on the safer side I have sent the application_url to verify.

APPLICATION_URL=http://localhost:8000

Ok, there is definitely an issue with CORS.

Lets add:

const cors = require("cors");
var corsOptions = {
  origin: "http://localhost:8000"
};
app.use('^(?!forest/?$).*', cors(corsOptions));

The whole file will be:

const env = process.env.NODE_ENV;
let debug = null;

if (env !== 'production') {
  const non_prod_config = require('dotenv').config();
  debug = require('debug')('index');
  debug(`app started in ${process.env.NODE_ENV} environment`);
  if (non_prod_config.error) {
    throw non_prod_config.error;
  }
} else {
  debug = require('debug')('index');
}
//const app = require('./app')
const express = require('express');
const app = express();
const routes = require('./routes/index');

const cors = require("cors");
var corsOptions = {
  origin: "http://localhost:8000"
};
app.use('^(?!forest/?$).*', cors(corsOptions));

process.on('unhandledRejection', (reason, p) => {
  debug('Unhandled Rejection at:', p, 'reason:', reason);

  process.exit(1);
  // Application specific logging, throwing an error, or other logic here
});

process.on('uncaughtException', err => {
  debug('Uncaught Exception');
  debug(err);

  process.exit(1);
});
app.use('/', routes);
const server = app.listen(8000, async () => {
  debug('server is runnning on port 8000');

  const forest = require('forest-express-sequelize');
  const db = require('./db/index');

  app.use(
    await forest.init({
      envSecret: process.env.FOREST_ENV_SECRET,
      authSecret: process.env.FOREST_AUTH_SECRET,
      objectMapping: db.Sequelize,
      connections: { default: db.sequelize }
    })
  );
});

Regarding your smart action routes, do not include the /forest prefix, it will be done by forest-express-sequelize internally.

1 Like
const env = process.env.NODE_ENV;
let debug = null;

if (env !== 'production') {
  const non_prod_config = require('dotenv').config();
  debug = require('debug')('index');
  debug(`app started in ${process.env.NODE_ENV} environment`);
  if (non_prod_config.error) {
    throw non_prod_config.error;
  }
} else {
  debug = require('debug')('index');
}
//const app = require('./app')
const express = require('express');
const app = express();
const routes = require('./routes/index');
const cors = require('cors')
const corsOptions = {
  origin: 'http://localhost:8000'
}
app.use('^(?!forest/?$).*', cors(corsOptions));
process.on('unhandledRejection', (reason, p) => {
  debug('Unhandled Rejection at:', p, 'reason:', reason);

  process.exit(1);
  // Application specific logging, throwing an error, or other logic here
});

process.on('uncaughtException', err => {
  debug('Uncaught Exception');
  debug(err);

  process.exit(1);
});

app.use('/', routes);
const server = app.listen(8000, async () => {
  debug('server is runnning on port 8000');

  const forest = require('forest-express-sequelize');
  const db = require('./db/index');

  app.use(
    await forest.init({
      envSecret: process.env.FOREST_ENV_SECRET,
      authSecret: process.env.FOREST_AUTH_SECRET,
      objectMapping: db.Sequelize,
      connections: { default: db.sequelize }
    })
  );
});

Still the cors error sir…

It seems the route for the smart action registers with the main express app and not with Forest.

Could you update the end of your server file with:

const server = app.listen(8000, async () => {
  debug('server is runnning on port 8000');

  const forest = require('forest-express-sequelize');
  const db = require('./db/index');

  const forestApp = await forest.init({
    envSecret: process.env.FOREST_ENV_SECRET,
    authSecret: process.env.FOREST_AUTH_SECRET,
    objectMapping: db.Sequelize,
    connections: { default: db.sequelize }
  })

  const forestRoutes = require('./routes/forest/index');

  forestApp.use('/forest', forestRoutes);

  app.use(forestApp);
});

You will need to create a new file routes/forest/index.js, with content (please double check model path and name):

const { PermissionMiddlewareCreator } = require('forest-express-sequelize');
const { card } = require('../../db/cards');

const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('cards');

router.post('/actions/non-league', permissionMiddlewareCreator.smartAction(), (req, res) => {
  console.log('/actions/non-league OK');
  return res.send({ success: '/actions/non-league OK' });
});

module.exports = router;

This may still need a few tweaks, but I have a project with a similar code working locally.

If I register the smart action route to the main express app, I get your initial ‘cannot read property’ issue, so I think this is a good lead.

1 Like
index Unhandled Rejection at: Promise {
  <rejected> TypeError: Cannot read property 'actions' of undefined
      at PermissionMiddlewareCreator._getSmartActionInfoFromRequest (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:52:62)
      at PermissionMiddlewareCreator._getPermissionsInfo (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:72:23)
      at _callee$ (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:102:43)
      at tryCatch (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\regenerator-runtime\runtime.js:63:40)
      at Generator.invoke [as _invoke] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\regenerator-runtime\runtime.js:293:22)     
      at Generator.next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\regenerator-runtime\runtime.js:118:21)
      at asyncGeneratorStep (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:3:24)      
      at _next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:25:9)
      at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:32:7
      at new Promise (<anonymous>)
      at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:21:12
      at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:130:23
      at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:95:5)       
      at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\route.js:137:13)
      at Route.dispatch (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\route.js:112:3)
      at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:95:5)       
      at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:281:22
      at Function.process_params (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:335:12)
      at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:275:10)
      at Function.handle (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:174:3)
      at router (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:47:12)
      at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:95:5)       
      at trim_prefix (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:317:13)
      at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:284:7
      at Function.process_params (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:335:12)
      at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:275:10)
      at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:91:12)      
      at trim_prefix (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:317:13)
      at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:284:7
      at param (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:354:14)
      at param (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:365:14)
      at Function.process_params (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:410:3)
      at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:275:10)
      at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express-jwt\lib\index.js:131:7
} reason: TypeError: Cannot read property 'actions' of undefined
    at PermissionMiddlewareCreator._getSmartActionInfoFromRequest (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:52:62)
    at PermissionMiddlewareCreator._getPermissionsInfo (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:72:23)
    at _callee$ (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:102:43)
    at tryCatch (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\regenerator-runtime\runtime.js:63:40)
    at Generator.invoke [as _invoke] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\regenerator-runtime\runtime.js:293:22)       
    at Generator.next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\regenerator-runtime\runtime.js:118:21)
    at asyncGeneratorStep (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:3:24)        
    at _next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:25:9)
    at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\@babel\runtime\helpers\asyncToGenerator.js:21:12
    at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\forest-express\dist\middlewares\permissions.js:130:23
    at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle [as handle_request] (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\layer.js:91:12)        
    at trim_prefix (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:284:7
    at param (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:354:14)
    at param (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:410:3)
    at next (C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express\lib\router\index.js:275:10)
    at C:\Users\Nikhilesh Ram\Desktop\visual studio\remote-config-dashboard\node_modules\express-jwt\lib\index.js:131:7 +15s

It is showing error…

There should be an inconsistency between the name of:

  • your sequelize model (look for sequelize.define('cards', ...) in your model file)
  • your Forest collection (collection('cards', ...)
  • the permission creator middleware (new PermissionMiddlewareCreator('cards'...)

I get the same error as yours when the parameter of PermissionMiddlewareCreator is not the same as the others.

1 Like

Yes yes there was inconsistency in the name now it is working perfectly fine. Thank you so much for your help for this project.

Regards,
Nikhilesh Ram

2 Likes

Glad it works, and sorry it took so long.

No no sir you don’t have to apologize, rather I am glad that you helped me out.