Create custom route

Hello,

I trying to override the user creation route.
I want to use my api to create the user.
I have managed to throw an error if something goes wrong by tweaking the user file in the route folder.
But I didn’t find a way to tell forest admin that everything went right and that it doesn’t need to create a user since I’ve already done it (sending a request with status 200 triggers an error).
Is it possible to do it ?

Thank you for your help,
Best,
Ilias

Hello @Ilias_El-mhamdi,

I am not sure to understand what you mean by “creating the user” yourself. Could you please elaborate? Maybe sharing the modifications you made to the user file would help.

Thank you

Hi

It was for my category table.
Here is what I’am trying to do.

routes/category.js


// Create a Category
router.post(
  "/category",
  permissionMiddlewareCreator.create(),
  async (request, response, next) => {
    // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#create-a-record
    console.log("create category");
    const categoryData = request.body.data;
    let departementId;
    try {
      departementId = categoryData.relationships.department.data.id;
    } catch (err) {}
    console.log(departementId, "dep id");
    let categoryToCreate = {
      departmentId: departementId,
    };
    try {
      //creating the category using my backend api 
      let res = await network.createCategory(categoryTocreate);
      console.log(res);
    } catch (err) {
      //error during the creation 
      response.status(500).send("can't create category");
      return null;
    }
    //telling forest that the creation was successful 
    response.status(200).send("category created");
    return null;
    next();
  }
);

Tell me if it’s not clear or if you need more infos
Thank you for your help and fast reply

Ilias

@Ilias_El-mhamdi,

Could you try to replace the end of you function by just

   response.status(200).send("category created");

If you compare you code with our documentation. The return and next statements are not required.

Do you have an error message to share?

I think you should also use the res value as a parameter to response.send to share the created record with your admin panel. It may need some data transformation to have the expected format.

Hello,
Thank you for your help.
It’s starting work when I give res as then send parameter. I have the notification creation success but I still get an error.


It may be because my res Json dosesn’t have enough info, I just have the id of the created category. And I see that in the documentation :
.then(recordSerialized => response.send(recordSerialized))
What info should be in the recordSerialized, the problem might lie there.

Thank you,
Best,
Ilias

This might add some processing delay, but you can try using the RecordGetter. See this: Smart field reference link

The process can be similar to:

  1. call await network.createCategory(categoryTocreate);
  2. call const record = await recordGetter.get(res.id) (or res depending on how the id is returned by your API)
  3. finish by response.send(recordGetter.serialize(record))

This is just guess-coding, but the idea is: create via you API, and then get it via Forest API and return it.

1 Like

It’s working !!!
I had to import
const {
RecordGetter,
} = require(“forest-express-sequelize”);
add
const recordGetter = new RecordGetter(category);
and add an await before the serialisation
const serialized = await recordGetter.serialize(record);
But it’s working !!

Thank you !

1 Like

I have the same problem but when i do
const recordGetter = new RecordGetter(category);
this gives an error:
Unexpected error: Cannot read property ‘timezone’ of undefined
how can i fix this?

Hi @mostafa_hatem !
Are you on v8 ? If you are, we revamped the scopes in this version and it brings some breaking changes, you can take a look at the documentation to see what you need to do.