Exporting custom segments doesn't include smart/custom fields

I created a custom segment, the data displays correctly; however, when I click the export to csv, the data is missing my custom fields.

Expected behavior

I expect forest admin to export the custom fields too.

Actual behavior

Export to csv should have all the data shown in the segment.

  • Express Version: ~4.16.3
  • Sequelize Version: ~5.15.1
  • Database Dialect: MySQL
  • Database Version:

Hi @james-janetech,

I’ve been unable to reproduce the issue so far, could you please share with me the content of the custom field definitions that are not exported correctly?

Also, you stated that the issue appears on segments. Does it also appear on the base collection export?

Thank you

Hi, @anon37102731

It doesn’t work in the base collection export either. The orders.js inside the forest directory. I have the following custom fields defined.

Hey @james-janetech,

I find it strange that orderItems is set :thinking:. Could you log it to see if it is set when exporting ?
If it is not set you should do something like:

order.getOrderItems().then(orderItems => { ... })

Order Items are being returned.
[model, model, model, model, model, model, model, model, model, model]

In my model.orders I have the following
Orders.hasMany(models.orderItems);

Inside of my model.order_items i have the following:
OrderItems.belongsTo(models.orders);

Order Items has the order_id field.

Just exporting doesn’t return anything.

With that said, I spent enough time troubleshooting and debugging. I decided to just add these fields to my database and have my applications calculate them.

Best, James

If you are able to come up with a solution, please share it.

Hi @james-janetech,

As we cannot reproduce the issue (ie we can export data with full Smart Fields values), it will be hard to find a solution without your help.

My first question is which version of the forest-express-sequelize agent does your server use?
If your server is up-to-date, then we need to dig into the units, total_price, sub_totat_price computation logic to understand why in the export context the values are not returned.
You can easily use debuggers / logs in the get functions of your Smart Fields, then trigger export actions and see what happens.

1 Like

@arnaud

server setup

    "chalk": "~1.1.3",
    "cookie-parser": "1.4.4",
    "cors": "2.8.5",
    "debug": "~4.0.1",
    "dotenv": "~6.1.0",
    "express": "~4.16.3",
    "express-jwt": "5.3.1",
    "forest-express-sequelize": "^6.0.0",
    "morgan": "1.9.1",
    "require-all": "^3.0.0",
    "sequelize": "~5.15.1",
    "mysql2": "~1.7.0"

@arnaud

After some debugging and looking at your example above, I was able to successfully debug and resolve issue.

New code

field: 'Units',
type: 'Number',
get: function(order) {
   return order.getOrderItems().then((orderItems) => {
     return orderItems.reduce((a, b) => {
       return a + (b['quantity'] || 0)
     }, 0)
  })
}

I’m able to see the units when exporting. I used the method getOrderItems().then((order_item))