BedfordNYGuy
BedfordNYGuy

Reputation: 403

I have a Typescript class where a method is not recognized

I have a Typesript class,AjaxBookStringParams. When I attempt to use the function/method BuildGenieParams, the error :"BookGenieLoader.ts:47 Uncaught TypeError: ajaxString.BuildGenieParams is not a function. I must be doing something very simple that this is not working. NOTE: It is the only method in any of my classes.

Here's the class:

export class AjaxBookStringParams {
AgeCandidates: string = ""; // AllocationCandidates by age to-from server
BookToDelete:  string = ""; // Used to delete an allocated book
AllocatedBooks:  string = "";; // placeholder for to-from server - grid display
BookGenieParams: string = "";
Data: string = "";
ProductFilterParams:  string = "";
Request: string = "";

constructor(ajaxParams:AjaxBookGenieParams){
    this.AgeCandidates = JSON.stringify(ajaxParams.AgeCandidates);
    this.BookToDelete = JSON.stringify(ajaxParams.AgeCandidates);
    this.AllocatedBooks = JSON.stringify(ajaxParams.AllocatedBooks);
    this.BookGenieParams = JSON.stringify(ajaxParams.BookGenieParams);
    this.ProductFilterParams = JSON.stringify(ajaxParams.ProductFilterParams);
    this.Data = ajaxParams.Data;
    this.Request = ajaxParams.Request;
}

BuildGenieParams():AjaxBookGenieParams{
    const ajaxBookGenieParams:AjaxBookGenieParams  = new  AjaxBookGenieParams();
    if(this.AgeCandidates != ""){ajaxBookGenieParams.AgeCandidates = JSON.parse(this.AgeCandidates)}
    if(this.BookToDelete != ""){ajaxBookGenieParams.BookToDelete = JSON.parse(this.BookToDelete)}
    if(this.AllocatedBooks != ""){ajaxBookGenieParams.AllocatedBooks = JSON.parse(this.AllocatedBooks)}
    if(this.BookGenieParams != ""){ajaxBookGenieParams.BookGenieParams = JSON.parse(this.BookGenieParams)}
    if(this.ProductFilterParams != ""){ajaxBookGenieParams.ProductFilterParams = JSON.parse(this.BookGenieParams)}  
    ajaxBookGenieParams.Data = this.Data;
    ajaxBookGenieParams.Request = this.Request;
    return ajaxBookGenieParams;
}

getJsonString():string{
    const jsonString = JSON.stringify({ajaxBookStringParams: AjaxBookStringParams,}) ;
    return jsonString;
}}

It is called by:

$(function () {
  // this is param load from initial page load - only used once
let element = document.getElementById("AjaxBookGenieParamsHold");
let htmlElement: HTMLElement = element as HTMLElement;
let tParam: string = htmlElement.innerHTML;
let ajaxString:AjaxBookStringParams = JSON.parse (tParam);
**let ajaxBookGenieParams:AjaxBookGenieParams = ajaxString.BuildGenieParams();**
BookGenieMethods.ajaxBookGenieParams = ajaxBookGenieParams; // initial load of values

The referenced Classes are:

export class AjaxBookGenieParams {
AgeCandidates: AgeCandidate[] = []; // AllocationCandidates by age to-from server
BookToDelete: Book = new Book(); // Used to delete an allocated book
AllocatedBooks: AllocatedBook[] = []; // placeholder for to-from server - grid display
BookGenieParams: BookGenieParams = new BookGenieParams();
Data: string = "";
ProductFilterParams: ProductFilterParams = new ProductFilterParams();
Request: string = "";}

export class ProductFilterParams {
RootNodes: RootNode[] = [];
FilterGuid: string = "";
AgeRange: AgeRange = new AgeRange();
SelectedAges: number[] = [];
ResetFilter: string = "";}

export class AgeRange {
    Min: number = 0;
    Max: number = 12;
    From: number = 0;
    FromInit: number = 0;
    To: number = 0;
    ToInit: number = 0;}

export class RootNode {
    Description: string = "";
    GroupType: string = "";
    Id: number = 0;
    Name: string = "";
    NodeCategories: NodeCategory[] = [];
    Selected: string = "";
    ToolTip: string = "";
    __expanded__: boolean = false;}

export class NodeCategory {
    Id: number = 0;
    Name: string = "";
    Count: number = 0;
    Selected: string = "";
    Description: string = "";}

export class BookGenieParams {
    AllocatedQuantity: number = 0;
    AllocatedSubTotal: number = 0;
    AllocatedTitles: number = 0;
    AllocationBudget: number = 200;
    BooksPerTitle: number = 5;
    CustomerGuid: string = "";
    CustomerId: number = 0;
    DefaultBooksPerTitle: number = 5;
    MinimumBudget: number = 200;
    MoveToCartSubTotal: number = 0;
    MoveToCartCount: number = 0;
    ReDisplayBreak: number = 10;
    SelectedCategories: number[] = [];  // Calculated from PFP
    UserBooksPerTitle: number = 0;}

export class AllocatedBook {
    AgeBooks: Book[] = [];
    AgeId: number = 0;
    Name: string = "";
    Titles: number = 0;
    Quantity: number = 0;
    SubTotal: number = 0}

export class Book {
    ISBN: string = "";
    ProductId: number = 0;
    ImageUrl: string = "";
    Title: string = "";
    Price: number = 0;
    Quantity: number = 0;
    AgeId: number = 0}

export class AgeCandidate {
    AgeId: number = 0;
    AllocationCandidates: AllocationCandidate[] = [];}

export class AllocationCandidate {
    AgeId: number = 0;
    DateLastTouched: Date = new Date;
    Inventory: number = 0;
    ISBN: string = "";
    Price: number = 0;
    ProdctId: number = 0;
    Title: string = "";
}

Upvotes: 0

Views: 588

Answers (1)

Matthieu Riegler
Matthieu Riegler

Reputation: 54943

JSON.parse returns any and you assigning its result to a variable of type AjaxBookStringParams. The compiler won't warn you.

Since you're only getting an object and not a AjaxBookStringParams instance, you will get the error you see at runtime.

You need to create a real AjaxBookStringParams instance from your parse result.

It'll probably be something like :

const ajaxString = new AjaxBookStringParams(JSON.parse (tParam));

Upvotes: 1

Related Questions