Smart Collection not displaying data

Hi,
I have created a smart collection, that retrieves some usage report data from an internal rest api but it will not show up in the forest admin ui.

I have created the a forest collection as so:

const { collection } = require("forest-express-sequelize");

collection("usage_report", {
  fields: [
    {
      field: "id",
      type: "String",
    },
    {
      field: "sms_current_month",
      type: "Number",
    },
    {
      field: "sms_previous_month",
      type: "Number",
    },
  ],
});

And a route as so:

const express = require("express");
const router = express.Router();
const { default: Axios } = require("axios");

const {
  RecordSerializer,
  PermissionMiddlewareCreator,
} = require("forest-express-sequelize");

const recordSerializer = new RecordSerializer({ name: "usage_report" });
const permissionMiddlewareCreator = new PermissionMiddlewareCreator(
  "usage_report"
);

router.get(
  "/usage_report",
  permissionMiddlewareCreator.list(),
  async (req, res, next) => {
    let report = {};
    try {
      report = await Axios({
        url: `${process.env.FOREST_ADMIN_API}/report`,
        method: "get",
        headers: {
          "content-type": "application/json",
        },
      });
    } catch (err) {
      next(err);
    }

    console.log(report.data);

    try {
      result = await recordSerializer.serialize(report.data);

      console.log(JSON.stringify(result, null, 4));

      res.send();
    } catch (err) {
      next(err);
    }
  }
);

module.exports = router;

When selecting Usage Report in the ui the following logs are generated:

forestadmin_1             | server-0  | [
forestadmin_1             | server-0  |   {
forestadmin_1             | server-0  |     id: 'de345a4f-dbc0-413a-8650-1277a3d4637a',
forestadmin_1             | server-0  |     sms_current_month: 0,
forestadmin_1             | server-0  |     sms_previous_month: 13,
forestadmin_1             | server-0  |     sms_two_months_ago: 5,
forestadmin_1             | server-0  |     mms_current_month: 0,
forestadmin_1             | server-0  |     mms_previous_month: 6,
forestadmin_1             | server-0  |     mms_two_months_ago: 0
forestadmin_1             | server-0  |   }
forestadmin_1             | server-0  | ]
forestadmin_1             | server-0  | {
forestadmin_1             | server-0  |     "data": [
forestadmin_1             | server-0  |         {
forestadmin_1             | server-0  |             "type": "usage_report",
forestadmin_1             | server-0  |             "id": "de345a4f-dbc0-413a-8650-1277a3d4637a",
forestadmin_1             | server-0  |             "attributes": {
forestadmin_1             | server-0  |                 "id": "de345a4f-dbc0-413a-8650-1277a3d4637a",
forestadmin_1             | server-0  |                 "sms_current_month": 0,
forestadmin_1             | server-0  |                 "sms_previous_month": 13
forestadmin_1             | server-0  |             }
forestadmin_1             | server-0  |         }
forestadmin_1             | server-0  |     ]
forestadmin_1             | server-0  | }
forestadmin_1             | server-0  | GET /forest/usage_report?fields%5Busage_report%5D=sms_current_month%2Csms_previous_month%2Cid&page%5Bnumber%5D=1&page%5Bsize%5D=15&searchExtended=0&sort=-id&timezone=Australia%2FSydney 200 - - 9.270 ms

Expected behavior

The data from the rest API should be displayed in forest admin

Actual behavior

The cannot reach data error page is shown

Context

  • Package Version: Latest liana
  • Express Version: express@4.17.1
  • Sequelize Version: forest-express-sequelize@6.6.3
  • Database Dialect: REST
  • Project Name: MTMO

Hi @Mike_Bissett, and welcome to our community

From what I see in your code, you are missing the res.send content in your get route code.

Your can also have a look to this part of the documentation, but I would say that replacing res.send(); by res.send({ ...result }); should fix the issue here.

Let me know if that helps :pray:

1 Like

Thank you for your response, I cant figure out how I missed something so obvious, because ive already serialized the result i just needed to change my res.send() to res.send(result).

2 Likes