question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OData not working and failing on 'statistics"

See original GitHub issue

Hi there,

I did ask an exploratory question on pagination and the idea was to get OData working on the server and then use the OData example to send through fetch queries to populate the dataset however I am having trouble getting this to work correctly having got OData to work and send back OData responses from the server.

I am getting the error: Uncaught (in promise) TypeError: Cannot set property 'statistics' of undefined at AureliaSlickgridCustomElement.<anonymous> (aurelia-slickgrid.js:496) at step (aurelia-slickgrid.js:48) at Object.next (aurelia-slickgrid.js:29) at fulfilled (aurelia-slickgrid.js:20)

My code is as follows and is a simple 5 column grid using OData.

        import { Aurelia, PLATFORM, autoinject } from "aurelia-framework";
        import { Router } from "aurelia-router";
        import { HttpClient, json } from 'aurelia-fetch-client';
        import { EventAggregator } from 'aurelia-event-aggregator';
        import {
            AureliaGridInstance,
            Column,
            FieldType,
            Formatters,
            Filters,
            GridOdataService,
            GridOption,
            OperatorType,
            SortDirection,
            Statistic,
        } from 'aurelia-slickgrid';

        import { MessagePayload } from '../../../../services/messages/messages'
        import { AuthService } from '../../../../services/auth/auth-service'

        const defaultPageSize = 20;
        const sampleDataRoot = 'src/examples/slickgrid/sample-data';

        @autoinject()
        export class ClientList {
            title = 'Client List';
            subTitle = `
        Click on a client to examine and or edit.
        <br/>
        `;
            aureliaGrid: AureliaGridInstance;
            columnDefinitions: Column[];
            gridOptions: GridOption;
            dataset = [];
            statistics: Statistic;

            odataQuery = '';
            processing = false;
            status = { text: '', class: '' };

            constructor(
                private http: HttpClient,
                private authService: AuthService,
                private router: Router) {
                // define the grid options & columns and then create the grid itself
                this.defineGrid();
            }

            aureliaGridReady(aureliaGrid: AureliaGridInstance) {
                this.aureliaGrid = aureliaGrid;
            }

            defineGrid() {
                this.columnDefinitions = [
                    { id: 'clientNo', name: 'Client No.', field: 'clientNo', sortable: true, type: FieldType.string, filterable: true, filter: { model: Filters.compoundInput } },
                    { id: 'company', name: 'Company', field: 'company', sortable: true, formatter: Formatters.checkmark },
                    { id: 'clientLastName', name: 'Last Name', field: 'clientLastName', sortable: true, type: FieldType.string, filterable: true, filter: { model: Filters.compoundInput } },
                    { id: 'clientFirstName', name: 'First Name', field: 'clientFirstName' },
                    { id: 'mobilePhone', name: 'Mobile Ph.', field: 'mobilePhone', sortable: true, type: FieldType.string, filterable: true, filter: { model: Filters.compoundInput } },


                ];

                this.gridOptions = {
                    enableAutoResize: true,
                    autoResize: {
                        containerId: 'demo-container',
                        sidePadding: 15
                    },
                    enableCellNavigation: true,
                    enableFiltering: true,
                    enableRowSelection: true,
                    pagination: {
                        pageSizes: [10, 20, 50, 100, 500],
                        pageSize: defaultPageSize,
                        totalItems: 0
                    },
                    presets: {
                        // you can also type operator as string, e.g.: operator: 'EQ'

                        sorters: [
                            // direction can be written as 'asc' (uppercase or lowercase) and/or use the SortDirection type
                            { columnId: 'clientNo', direction: 'asc' },
                            { columnId: 'clientLastName', direction: 'asc' },
                            { columnId: 'mobilePhone', direction: 'asc' }
                        ],
                        pagination: { pageNumber: 1, pageSize: 10 }
                    },
                    backendServiceApi: {
                        service: new GridOdataService(),
                        preProcess: () => this.displaySpinner(true),
                        process: (query) => this.getCustomerApiCall(query),
                        postProcess: (response) => {
                            //console.log("RESPONSE: ", response);
                            //this.statistics = response.statistics;
                            this.displaySpinner(false);
                            this.getCustomerCallback(response);
                        }
                    }
                };
            }

            displaySpinner(isProcessing) {
                this.processing = isProcessing;
                this.status = (isProcessing)
                    ? { text: 'processing...', class: 'alert alert-danger' }
                    : { text: 'done', class: 'alert alert-success' };
            }

            getCustomerCallback(data) {
                // totalItems property needs to be filled for pagination to work correctly
                // however we need to force Aurelia to do a dirty check, doing a clone object will do just that
                console.log("...AND HERE!");
                this.gridOptions.pagination.totalItems = data['totalRecordCount'];
                this.gridOptions = { ...{}, ...this.gridOptions };
                if (this.statistics) {
                    this.statistics.totalItemCount = data['totalRecordCount'];
                }

                // once pagination totalItems is filled, we can update the dataset
                this.dataset = data['items'];
                this.odataQuery = data['query'];
            }

            getCustomerApiCall(odataQuery) {
                // in your case, you will call your WebAPI function (wich needs to return a Promise)
                // for the demo purpose, we will call a mock WebAPI function
                const headers = this.authService.header();

                return fetch(`/api/Client/Index?${odataQuery}`, {
                    method: "GET",
                    headers
                }).then(response => {
                    return response.json();
                });
            }
        }

For completeness here is the controller in the dotnet app:

   [HttpGet("Index", Name = "ClientIndex")]
   [EnableQuery()]
	public IEnumerable<Client> GetClients() {
		return _context.Clients;
	}

The dataset coming back from this is:

dataset:
DATASET

I see that it is erroring on statistics as if the return set should have “totalRecordCount” and it doesnt so its identifying that as undefined I think…

I am not sure how to get this working once but if I can I believe I can use it throughout the app which is what I want to do…

So, what am I missing? Its using the query created by slickgrid etc…

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
si2030commented, Aug 4, 2019

I am really sorry regarding my question and the fact it was such a stupid error - i’d be frustrated. The last thing I want to do is cause people like you to allocate time you cant get back. That said I really want to use your work to decorate my project… Its excellent.

To this end I thought under the circumstances I would throw you a few coffees… as I am still struggling with this implementation as its failing on value converters now sadly… however once I have it working once I will be sorted across the whole project… again sorry to bother. 😉

PS whilst I want this project done its fine to leave me go a few days (fix the bugs) as I am it on the project and not relying on this for a job…

0reactions
ghiscodingcommented, Aug 12, 2019

Fixed now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Odata Filter Query Not Working - Power Platform Community
The workflow in the "Roles" list has a "Get Items" action that is attempting to pull data from the "Role Creation" list. The...
Read more >
oData 8.0.3 with .NET6 reports an error when the query uses ...
We upgraded oData to 8.3 and we have a serious problem. Queries that use $expand, $select do not work anymore, used to work...
Read more >
Odata Fails… Sometimes • Acumatica OData with Microsoft ...
Ok, I believe the problem has to do with how OData works and some time-out setting must exist somewhere. Odata sets with small...
Read more >
Odata query generation problems - Forums - IBM Support
Hi, The syntax error is caused by the 'nvarchar(0)' in the generated query. APAR IC87451 is already logged and should be fixed in...
Read more >
EntitySetController $expand and $select not working
However all of my requests for $select and $expand fail. Any ideas? I'm using Entity Framework v6 and System.Web.Http.OData v5. My entity: public ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found