vendredi 1 octobre 2021

TS1108 (TS) A 'return' statement can only be used within a function body - knockout js, durandal, typescript

I've been tasked with updating an application that was written in 2015. It hasn't been touched since then. I pulled the Solution into VS 2019 and after figuring out a new NuGet errors and modifying the ajax calls I'm left with an error I'm not familiar with. I can't find anything on Google and I've looked all over this site as well. I've never used durandal before and have limited knockout experience. I know this worked and compiled and it runs now on the server but won't compile or run for me now. If I need to add anything else please let me know. There are three of these models with the same error. Thanks all!

This is saved under a folder named viewmodels as subscriptions.ts.

/// <reference path="../../scripts/typings/knockout.validation/knockout.validation.d.ts" />
/// <reference path="../../scripts/typings/bootstrap/bootstrap.d.ts" />

import system = require("durandal/system");
import settings = require("settings");
import models = require("models");

class SubscriptionsViewModel {
    eventNames: KnockoutObservableArray<string>;
    eventName: KnockoutObservable<string>;
    pagination: models.Pagination;
    searchMessage: KnockoutObservable<string>;
    subscriptionCount: KnockoutObservable<number>;
    subscriptions: KnockoutObservableArray<models.Subscription>;
    filteredSubscriptions: KnockoutObservableArray<models.Subscription>;
    selectedSubscription: KnockoutObservable<models.Subscription>;
    subscriptionHeaders: Array<models.GridHeader>;

    constructor() {
        this.eventNames = ko.observableArray([]);
        this.eventName = ko.observable("");
        this.pagination = new models.Pagination();
        this.searchMessage = ko.observable("");
        this.subscriptionCount = ko.observable(0);
        this.subscriptions = ko.observableArray([]);
        this.filteredSubscriptions = ko.observableArray([]);

        this.selectedSubscription = ko.observable(new models.Subscription({}));

        this.subscriptionHeaders = [
            new models.GridHeader({ title: '', sortPropertyName: '', columnType: 'image' }),
            new models.GridHeader({ title: 'Event Name', sortPropertyName: 'eventName', columnType: 'string' }),
            new models.GridHeader({ title: 'Has XSLT', sortPropertyName: 'hasXSLT', columnType: 'string' })
        ];

        this.pagination.itemsPerPage.subscribe((newValue: number): void => {
            this.searchSubscriptions();
        });

        this.pagination.currentPage.subscribe((newValue: number): void => {
            this.searchSubscriptions();
        });
    }

    //#region Search Subscriptions
    searchSubscriptions = (): void => {
        var countUri = settings.serviceUri + "api/Subscription/Count/";
        var subscriptionUri = settings.serviceUri + "api/Subscription/?top=" + this.pagination.top() + "&skip=" + this.pagination.skip();

        if (this.eventName().length > 0) {
            countUri = settings.serviceUri + "api/Subscription/Count/StartsWith/" + this.eventName();
            subscriptionUri = settings.serviceUri + "api/Subscription/StartsWith/" + this.eventName() + "?top=" + this.pagination.top() + "&skip=" + this.pagination.skip();
        }

        $.ajax({
            url: countUri,
            type: "GET",
            dataType: "json"
        }).then((data: any): JQueryXHR => {
                this.subscriptionCount(data);
                this.pagination.totalItems(data);

                return $.ajax({
                    url: subscriptionUri,
                    type: "GET",
                    dataType: "json"
                });
            }).done((data: any): void => {
                var subscriptions = new Array<models.Subscription>();
                for (var i = 0; i < data.length; i++) {
                    subscriptions.push(new models.Subscription(data[i]));
                }

                this.filteredSubscriptions(subscriptions);

                if (this.filteredSubscriptions().length == 0) {
                    this.searchMessage("No subscriptions found!");
                }
                else {
                    this.searchMessage(this.filteredSubscriptions().length + " subscriptions (" + this.subscriptionCount() + " total)");
                    this.pagination.pageMessage("Page " + this.pagination.currentPage() + " of " + this.pagination.totalPages());
                }
            });
    }
    //#endregion

    //#region Get Subscriptions
    getSubscriptions = (): void => {
        if (this.eventNames().length == 0) {
            $.ajax({
                url: settings.serviceUri + 'api/Subscription',
                //method: 'GET',
                type: 'GET',
                dataType: 'json'
            }).done((data: any): void => {
                    for (var i = 0; i < data.length; i++) {
                        this.eventNames.push(data[i].EventName.trim());
                        this.subscriptions.push(data[i]);
                    }
                });
        }
    }
    //#endregion

    populateSubscriptionDetails = (data: any): void => {
        this.selectedSubscription(data);
        $('#subscriptionDetailsModal').modal();
    }
}

var vm: SubscriptionsViewModel = new SubscriptionsViewModel();

return {
    displayName: 'Subscriptions',
    description: 'ENS Subscriptions',
    attached: (): void => {
        vm.getSubscriptions();
    },
    eventNames: vm.eventNames,
    eventName: vm.eventName,
    pagination: vm.pagination,
    searchSubscriptions: vm.searchSubscriptions,
    searchMessage: vm.searchMessage,
    subscriptions: vm.subscriptions,
    filteredSubscriptions: vm.filteredSubscriptions,
    subscriptionHeaders: vm.subscriptionHeaders,
    populateSubscriptionDetails: vm.populateSubscriptionDetails,
    selectedSubscription: vm.selectedSubscription
}

This is the error and all it tells me:

Error: TS1108 (TS) A 'return' statement can only be used within a function body




Aucun commentaire:

Enregistrer un commentaire