Hi @anon34731316,
I’m running into the same issue mentioned in this article where Field representing related object not populating
Order model association:
Orders.associate = (models) => {
Orders.belongsTo(models.buyers);
Orders.belongsTo(models.collections);
Orders.hasMany(models.orderItems);
};
My order smart view
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('orders', {
actions: [],
fields: [
{
field: 'order number',
type: 'Integer',
get: (order) => {
return order.id
}
},
{
field: 'units',
type: 'Number',
get: function(order) {
return order.orderItems.reduce((a, b) => {
return a + (b['quantity'] || 0)
}, 0);
}
},
{
field: 'Collection Name',
type: "String",
get: (order) => {
return `${order.collection.brand.name} ${order.collection.season}`;
}
},
{ // used to override totalPrice until moved to the database
field: 'total_price',
type: 'String',
get: (order) => {
let totalPrice = (order.status == 'draft') ? (order.orderItems||[]).reduce((a, b) => { return a + (parseFloat(b['totalPrice']) || 0) }, 0) : parseFloat(order.totalPrice);
let currency = (order.orderItems.length > 0) ? order.orderItems[0].currency : order.collection.currency[0]
return `${totalPrice.toFixed(2)} ${currency.toUpperCase()}`
}
},
{ // used to override subtotalPrice // used to override totalPrice until moved to the database
field: 'sub_total_price',
type: 'String',
get: (order) => {
let subtotalPrice = (order.status == 'draft') ? (order.orderItems||[]).reduce((a, b) => { return a + (parseFloat(b['subtotalPrice']) || 0) }, 0) : parseFloat(order.subtotalPrice);
let currency = (order.orderItems.length > 0) ? order.orderItems[0].currency : order.collection.currency[0]
return `${subtotalPrice.toFixed(2)} ${currency.toUpperCase()}`
}
}
],
segments: [],
});
I’m only able to view the see the smart fields if I click on the data.
This tells me my orders list isn’t including the joins when querying. I even tried it using the snipet here
https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship