Action cannot be reached

Dear Forestadmin Support Team,

i’m met a problem with smart action: always display “… action cannot be reached”.
example:
#1 I have this table, which will update cdt_status column to “Active” value after i execute a smart “Active” Action:


#2 Setting enable smart action:

#3 Define function on route:

But after all setting it always display “Active action cannot be reached”
Pls help me review the my implementation, where is wrong?

Thanks & Regards!
Hung Chu

1 Like

Hi there @chumanhhung235 :wave:t3:

There seems to be a small typo in your route, try this path instead

router.post('/actions/active', permissionMiddlewareCreator.smartAction(), (request, response) =>  { 
// ...
});

This is not required but I also recommend setting a type for your action - single, bulk, or global.

Happy hacking :vulcan_salute:t3:

Thank @anon20071947, i updated “/action” => “/actions” but when run it throws error message in “[forest] :deciduous_tree::deciduous_tree::deciduous_tree: Unexpected error: RecordsGetter is not defined
ReferenceError: RecordsGetter is not defined”
pls help me continuous :).

1 Like

Sure, you need to import the RecordsGetter class:

const { PermissionMiddlewareCreator, RecordsGetter } = require('forest-express-sequelize');

One last thing, I have a doubt about your cdt_status field in the action’s definition. What does your cdt model look like (models/cdt.js)? Shouldn’t the field be cdtStatus instead?

Let me know if this helps :+1:t3:

oh, surprise i’m wrong cdt_status too. I’ve updated “cdt_status” => “cdtStatus”
i’m feeling it works but it have small problem still as image below:


“Active is executing…” display very long time & don’t change cdtStatus value from “Inactive” to “Active”.
pls help me review error logs.

Thanks so much!
Hung Chu

It’s not exactly clear to me what’s wrong here. Can you please share the code snippets of (not screenshots, use the </> code block from the rich text editor):

  • Your models/cdt.js file
  • Your routes/cdt.js file
  • The complete error logs from the backend, the first bit instead of the last one

Thanks :pray:

Dear @anon20071947,

  • My models/cdt.js file:

// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
// This section contains the fields of your model, mapped to your table’s columns.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const Cdt = sequelize.define(‘cdt’, {
cdtId: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true,
},
cdtName: {
type: DataTypes.STRING,
allowNull: false,
},
cdtNameEn: {
type: DataTypes.STRING,
},
cdtCode: {
type: DataTypes.STRING,
allowNull: false,
},
cdtCodeLong: {
type: DataTypes.STRING,
},
cdtValidFrom: {
type: DataTypes.DATE,
},
cdtValidTo: {
type: DataTypes.DATE,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: Sequelize.literal(‘CURRENT_TIMESTAMP’),
},
cdtStatus: {
type: DataTypes.STRING,
allowNull: false,
},
}, {
tableName: ‘cdt’,
underscored: true,
timestamps: false,
});
// This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
Cdt.associate = (models) => {
Cdt.belongsTo(models.user, {
foreignKey: {
name: ‘cdtOwnerKey’,
field: ‘cdt_owner’,
},
targetKey: ‘userId’,
as: ‘cdtOwner’,
});
Cdt.belongsTo(models.cdt, {
foreignKey: {
name: ‘cdtIdParentKey’,
field: ‘cdt_id_parent’,
},
targetKey: ‘cdtId’,
as: ‘cdtIdParent’,
});
Cdt.hasMany(models.cdt, {
foreignKey: {
name: ‘cdtIdParentKey’,
field: ‘cdt_id_parent’,
},
as: ‘cdtIdParentCdts’,
});
};
return Cdt;
};

  • My routes/cdt.js file:

const express = require(‘express’);
const { PermissionMiddlewareCreator, RecordsGetter } = require(‘forest-express-sequelize’);
const { cdt } = require(‘…/models’);
const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator(‘cdt’);
// This file contains the logic of every route in Forest Admin for the collection cdt:
// - Native routes are already generated but can be extended/overriden - Learn how to extend a route here: Extend a route - Developer Guide
// - Smart action routes will need to be added as you create new Smart Actions - Learn how to create a Smart Action here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/actions/create-and-manage-smart-actions
// Create a Cdt
router.post(‘/cdt’, permissionMiddlewareCreator.create(), (request, response, next) => {
// Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#create-a-record
next();
});
// Update Status: Set Active
router.post(‘/actions/active’, permissionMiddlewareCreator.smartAction(), (request, response) => {
return new RecordsGetter(cdt).getIdsFromRequest(request)
.then((cdtIds) => {
return cdt
.update({cdtStatus:‘Active’}, {where: { id: cdtIds }})
.then(() => {
response.send({success: ‘CDT is now Active!’});
});
});
});
// Update a Cdt
router.put(‘/cdt/:recordId’, permissionMiddlewareCreator.update(), (request, response, next) => {
// Learn what this route does here: Default routes - Developer Guide
next();
});
// Delete a Cdt
router.delete(‘/cdt/:recordId’, permissionMiddlewareCreator.delete(), (request, response, next) => {
// Learn what this route does here: Default routes - Developer Guide
next();
});
// Get a list of Cdts
router.get(‘/cdt’, permissionMiddlewareCreator.list(), (request, response, next) => {
// Learn what this route does here: Default routes - Developer Guide
next();
});
// Get a number of Cdts
router.get(‘/cdt/count’, permissionMiddlewareCreator.list(), (request, response, next) => {
// Learn what this route does here: Default routes - Developer Guide
next();
});
// Get a Cdt
router.get(‘/cdt/:recordId’, permissionMiddlewareCreator.details(), (request, response, next) => {
// Learn what this route does here: Default routes - Developer Guide
next();
});
// Export a list of Cdts
router.get(‘/cdt.csv’, permissionMiddlewareCreator.export(), (request, response, next) => {
// Learn what this route does here: Default routes - Developer Guide
next();
});
// Delete a list of Cdts
router.delete(‘/cdt’, permissionMiddlewareCreator.delete(), (request, response, next) => {
// Learn what this route does here: Default routes - Developer Guide
next();
});
module.exports = router;

Pls help me review them, where is wrong?

Thanks & Regards!

1 Like

Hello @chumanhhung235 :wave:

models/cdt.js and routes/dct.js look right :grinning_face_with_smiling_eyes:

Could you please post the complete error trace from your server?

Looks like the error is from type UnhandledPromiseRejectionWarning, but I need more info to help you.