Smart field to join firstName and lastName not working

Hi,

I want to create a smart field by joining first name and last name in the admin panel. I followed the instructions from the documentation but it is not working. I am not sure about what I am missing ?

I am using among others:
@forestadmin/agent”: “^1.36.14”,
@forestadmin/datasource-mongoose”: “^1.5.33”,
“forest-express-mongoose”: “^9.3.14”,

// // **** user.js under forest folder

const { collection } = require("forest-express-mongoose");
const { User } = require("../startup/db").models;

collection("User", {
  fields: [
    {
      field: "fullName",
      type: "String",
      get: (user) => {
        return user.firstName + " " + user.lastName;
      },
      set: async (user, fullName) => {
        const userBeforeUpdate = await User.findById(user.id);
        const names = fullName.split(" ");
        user.firstname = `${names[0]}`;
        return user;
      },
    },
  ],
});

Hello @Boubacar,

Thanks for reaching out and welcome to our community :raised_hands:

You said it’s not working: what do you observe actually?
Can you see the fullName field in your UI? In your forestadmin-schema?
Is it the value that is not right? Did you try to put some logs in your code?

Cheers.

Hello @anon34731316 ,

Thank you for your reply.

What I mean by not working is that I dont’ see the field “fullName” in the admin panel UI. Besides, the field “fullName” is also not preset in .forestadmin-schema.json file.
I tried to put some logs in the part related to “get” key in the code, but there is no log which means that part of the code is not executed…

I followed the example as in the documentation but the UI is not showing what is expected…

Best,

Alright, first thing is to make it appear in your forestadmin-schema.

Did you restart your agent?
Are you working on a Forest development environment?

A colleague just raised that there is something strange with what you shared: you mentioned both “@forestadmin/datasource-mongoose” (new system based on @forestadmin/agent) and “forest-express-mongoose” (historical system) which should not happen.

Could you please share your project name?

You are probably not on the right documentation.
If you’ve installed Forest Admin with @forestadmin/agent (new system), here is the doc you should refer to.

1 Like

Hi @anon34731316 ,

Thank you so much !
It’s now working and I have the new field “fullName” in both the UI of the admin panel and forestadmin-schema.

Indeed, I am using @forestadmin/agent and the problem was that I was not using the right doc as you pointed out. :wave:
Perhaps, it would be to good to point out that in the doc. regarding “forest-express-mongoose” …

Again, thanks :+1:

1 Like