Smart field does no appear in dev

Hello there, i’ve added a smart field to display the number of bookings my user have in database.

const { collection } = require('forest-express-sequelize');
const models = require('../models');


// This file allows you to add to your Forest UI:
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments
collection('user', {
  actions: [],
  fields: [{
    field: 'count_bookings',
    type: 'Number',
    reference: 'booking.id',
    defaultValue: 0,
    get: function (user) {
      console.log("user.id",user.firstname);
      // return 0;
      return models.booking
        .findAll({
          include: [{
            model: models.user,
            where: { id: user.id }
          }]
        })
        .then((bookings) => {
          console.log("bookings",bookings.length);
          return bookings.length;
        });
    }
  }],
  segments: [],
});

console.log show that there is data returned

user.id Truong
user.id David
bookings 1
bookings 0

but nothing is shown on the dashboard
Capture d’écran 2022-07-12 à 16.45.17

Package.json
“dependencies”: {
“body-parser”: “^1.20.0”,
“chalk”: “^1.1.3”,
“cookie-parser”: “^1.4.4”,
“cors”: “^2.8.5”,
“debug”: “^4.0.1”,
“dotenv”: “^6.1.0”,
“express”: “^4.17.1”,
“express-jwt”: “^6.0.0”,
“forest-express-sequelize”: “^8.5.10”,
“morgan”: “^1.9.1”,
“pg”: “^8.2.2”,
“require-all”: “^3.0.0”,
“sequelize”: “~5.15.1”,
“stripe”: “^9.0.0”
}

Hey @Jules_Truong :wave:

Could you:

  • Provide your project name, so I can check on my end if I can spot something
  • If possible, provide both the request payload & response body of the /user call (Visible in your network tabs in your browser developer console)
  • Validate that your smart field in indeed visible in your .forestadmin-schema.json?

Thanks in advance

projectname : rayon
request payload:

timezone=Europe%2FParis&fields%5Baddress%5D=id&fields%5Bcount_bookings%5D=id&fields%5Buser%5D=id%2Cfirstname%2Caddress%2CauthId%2Ccount_bookings%2Cemail%2Cphonenumber%2Clastname%2Croles%2Cstatus%2CstripeRef%2CemailHash%2CemailIsValidated%2CcreatedAt%2CupdatedAt&page%5Bnumber%5D=1&page%5Bsize%5D=15&sort=-id

response payload:

"relationships":{
            "count_bookings":{
               "data":null,
               "links":{
                  "related":{
                     "href":"/forest/user/2/relationships/count_bookings"
                  }
               }
            },

.forestadmin-schema.json contains the smart field

I’m guessing this is not the full response payload, right?
Eventually, could you try removing the reference: booking.id in the smart field definition?

In your case, the response is not particulary related to this reference collection, as you fetch the models.booking in the implementation.

1 Like

ok it works ! thank you

1 Like