0

I am using @grapecity/activereports-react in my react app. I am using my mongodb database as datasource. I created Server and endpoint to read my data from db.

// Define an endpoint that retrieves data from each collection
app.get('/:collectionName', async (req, res) => {
  try {
    const collectionName = req.params.collectionName;
    const collection = mongoose.connection.collection(collectionName);
    const data = await collection.find().toArray();
    res.json(data);
  } catch (err) {
    console.error(err);
    res.status(500).send('Server error');
  }
});

I defined my datasource ;

  const Jforce_DataSource = {
    Name: 'Jforce',
    ConnectionProperties: {
      DataProvider: 'JSON',
      ConnectString: 'endpoint=http://localhost:3001/' ,
    },
  };

  const dataSources = [
    {
      id: 'Jforce',
      title: 'Jforce',
      template: Jforce_DataSource,
      canEdit: false,
      datasets: collections.map((collection) => {
        return {
          id: collection,
          title: collection,
          template: {
            Name: collection,
            Query: { DataSourceName: 'Jforce', CommandText: `uri=/${collection};jpath=$.[*]` },
            Fields: [
              { Name: '_id', DataField: '_id' },
              { Name: 'Date', DataField: 'Run.Date' },
              { Name: 'Index', DataField: 'Run.Index' },
              { Name: 'ScenarioName', DataField: 'Run.Scenario.Name' },
              { Name: 'ScenarioDatabase', DataField: 'Run.Scenario.Database' },
              { Name: 'SvnAddress', DataField: 'Run.Svn.Address' },
              { Name: 'SvnRevision', DataField: 'Run.Svn.Revision' },

              { Name: 'PlatformProfile', DataField: 'Statistics.Platforms.Profile' },
              { Name: 'PlatformType', DataField: 'Statistics.Platforms.Type' },
              { Name: 'PlatformForce', DataField: 'Statistics.Platforms.Force' },
              { Name: 'PlatformTotal', DataField: 'Statistics.Platforms.Total' },
              { Name: 'PlatformWrecked', DataField: 'Statistics.Platforms.Wrecked' },
              { Name: 'PlatformDestroyed', DataField: 'Statistics.Platforms.Destroyed' },
            ],
          },
          canEdit: true,
        };
      }),
    },
  ];

My data is nested. I can retrieve _id,Run.Date,Run.Index,Run.Scenario.Name ,Run.Scenario.Database, Run.Svn.Address and Run.Svn.Revision but I can not get Statistics.Platforms,Statistics.Weapons ,Variables or Measurements. designer viewer

Here is my nested data ;


{
  "_id": {
    "$oid": "639714f6dc06372526070821"
  },
  "Run": {
    "Date": {
      "$date": "2022-12-12T11:48:06Z"
    },
    "Index": 1,
    "Scenario": {
      "Name": "YZU_senaryo1",
      "Database": "default"
    },
    "Svn": {
      "Address": "",
      "Revision": 0
    }
  },
  "Statistics": {
    "Platforms": [
      {
        "Profile": "F",
        "Type": "W",
        "Force": "B",
        "Total": 1,
        "Wrecked": 0,
        "Destroyed": 0
      },
      {
        "Profile": "F",
        "Type": "W",
        "Force": "B",
        "Total": 1,
        "Wrecked": 0,
        "Destroyed": 0
      },
      {
        "Profile": "O",
        "Type": "S",
        "Force": "R",
        "Total": 1,
        "Wrecked": 0,
        "Destroyed": 0
      }
    ],
    "Weapons": [
      {
        "Profile": "S",
        "Type": "M",
        "Force": "B",
        "Total": 2,
        "Hit": 0,
        "Missed": 2
      },
      {
        "Profile": "R",
        "Type": "M",
        "Force": "R",
        "Total": 2,
        "Hit": 2,
        "Missed": 0
      }
    ]
  },
  "Variables": [
    {
      "Type": "PlatformR",
      "Configuration": "InitialValue: 0",
      "Vaue": 0
    },
    {
      "Type": "PlatformR",
      "Configuration": "InitialValue: 30",
      "Vaue": 30
    },
    {
      "Type": "PlatformD",
      "Configuration": "InitialValue: 50000",
      "Vaue": 50000
    },
    {
      "Type": "PlatformD",
      "Configuration": "InitialValue: 50000",
      "Vaue": 50000
    },
    {
      "Type": "WeaponR",
      "Configuration": "InitialValue: 10000",
      "Vaue": 50000
    },
    {
      "Type": "LoadedW",
      "Configuration": "InitialValue: 1",
      "Vaue": 1
    },
    {
      "Type": "LoadedW",
      "Configuration": "InitialValue: 1",
      "Vaue": 1
    }
  ],
  "Measurements": [
    {
      "Type": "WEAPON_L",
      "Configuration": " [Entity:Destroyer]",
      "Value": 0,
      "Details": {}
    },
    {
      "Type": "WEAPON_L",
      "Configuration": " [Force:RED]",
      "Value": 2,
      "Details": {
        "AverageRange": 24813,
        "Range": [
          24880,
          24747
        ]
      }
    },
    {
      "Type": "WEAPON_H",
      "Configuration": " [Force:BLUE]",
      "Value": 0,
      "Details": {}
    },
    {
      "Type": "WEAPON_I",
      "Configuration": " [Force:BLUE]",
      "Value": 2,
      "Details": {
        "AverageRange": 14375,
        "Range": [
          14420,
          14329
        ]
      }
    },
    {
      "Type": "PLATFORM",
      "Configuration": " [Entity:Destroyer]",
      "Value": 0,
      "Details": {}
    },
    {
      "Type": "WEAPON",
      "Configuration": " [Force:BLUE]",
      "Value": 2,
      "Details": {
        "AverageRange": 49913,
        "Range": [
          50166,
          49661
        ]
      }
    },
    {
      "Type": "WEAPON",
      "Configuration": " [Force:BLUE]",
      "Value": 0,
      "Details": {}
    }
  ]
}


I have tried ;

          { Name: 'PlatformProfile', DataField: 'Statistics.Platforms[*].Profile' },
          { Name: 'PlatformType', DataField: 'Statistics.Platforms[*].Type' },
          { Name: 'PlatformForce', DataField: 'Statistics.Platforms[*].Force' },
          { Name: 'PlatformTotal', DataField: 'Statistics.Platforms[*].Total' },
          { Name: 'PlatformWrecked', DataField: 'Statistics.Platforms[*].Wrecked' },
          { Name: 'PlatformDestroyed', DataField: 'Statistics.Platforms[*].Destroyed' },

and

          { Name: 'WeaponProfile', DataField: 'Statistics.Weapons[0].Profile' },
          { Name: 'WeaponType', DataField: 'Statistics.Weapons[0].Type' },
          { Name: 'WeaponForce', DataField: 'Statistics.Weapons[0].Force' },
          { Name: 'WeaponTotal', DataField: 'Statistics.Weapons[0].Total' },
          { Name: 'WeaponHit', DataField: 'Statistics.Weapons[0].Hit' },
          { Name: 'WeaponMissed', DataField: 'Statistics.Weapons[0].Missed' },

They did not work. I will be glad if you help.

EDIT: I changed server side ;

app.get('/:collectionName', async (req, res) => {
  try {
    const collectionName = req.params.collectionName;
    const collection = mongoose.connection.collection(collectionName);
    const data = await collection.aggregate([
      { $unwind: '$Statistics.Platforms' },
      { $project: {
          _id: 1,
          'Run.Date': 1,
          'Run.Index': 1,
          'Run.Scenario.Name': 1,
          'Run.Scenario.Database': 1,
          'Run.Svn.Address': 1,
          'Run.Svn.Revision': 1,
          'Statistics.Platforms.Profile': 1,
          'Statistics.Platforms.Type': 1,
          'Statistics.Platforms.Force': 1,
          'Statistics.Platforms.Total': 1,
          'Statistics.Platforms.Wrecked': 1,
          'Statistics.Platforms.Destroyed': 1,
          'Statistics.Weapons.Profile':1,
          'Statistics.Weapons.Type' :1,
          'Statistics.Weapons.Force' :1,
          'Statistics.Weapons.Total' :1 ,
          'Statistics.Weapons.Hit' :1 ,
          'Statistics.Weapons.Missed' :1

        }
      },
    ]).toArray();
    res.json(data);
  } catch (err) {
    console.error(err);
    res.status(500).send('Server error');
  }
});

Now I can retrieve Statistics.Platforms data but when I add

{ $unwind: '$Statistics.Platforms' },
{ $unwind: '$Statistics.Weapons' },

output is wrong. I have 21 Statistics.Platforms.Total but with this code I see 42 Statistics.Platforms.Total variable. Why ?

user
  • 1
  • 3

0 Answers0