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
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”
}