Base solution for your next web application
Open Closed

nswag TypeError:this.birthDate.toISOString is not a function #3077


User avatar
0
justinp created

I noticed when I added a birthdate to a class and created an AppService with public DateTime BirthDate { get; set; }, swagger will generate code that looks like this:

toJS(data?: any) { data = data === undefined ? {} : data; data["active"] = this.active !== undefined ? this.active : null; data["birthDate"] = this.birthDate ? this.birthDate.toISOString() : null; return data; }

The problem is that this generates the following javascript error: ERROR TypeError: this.birthDate.toISOString is not a function

I have to manually change the line of code from .toISOString() to .toString() to fix the problem.

Is there a way to have swagger create the correct code? Should I be doing something special with dates when I create my app service?

Thanks


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    that method is defined in momentjs I think and we include momentjs to generated proxies here <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/angular/nswag/service.extensions.ts">https://github.com/aspnetzero/aspnet-ze ... ensions.ts</a>.

    Do you get this error on runtime or in visual studio compile time ?

  • User Avatar
    0
    justinp created

    I get the error at runtime when trying to save a new person. If you follow the step-by-step tutorial, where you add a person and phones, and also add a birthdate, then you will no longer be able to save and you get the error I included in the browser console.

    I also checked the generated service-proxies class and it has:

    birthDate: moment.Moment;

    Other classes also use Moment for dates, but it's unclear from the error why this Moment is not allowed to use the toISOString method.

    Just a note: I also set a breakpoint and the API is never hit, which is expected.

    Thoughts?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry for my late response. It seems like when you bind date field of your client side object to an input's model, it is considered as string and you get that error. In order to fix this, you can take a look at Old Answer section of this post <a class="postlink" href="http://stackoverflow.com/a/37055451">http://stackoverflow.com/a/37055451</a>.

    If you use a datetimepicker, it should set birthDate property of person as a moment object in the client side and this error should not happen in that case.

    Thanks.

  • User Avatar
    0
    jbalda created

    Hi,

    I have the same problem. I've implemented with pipes:

    <input type="date" #dateBirthInput class="form-control" name="datBirth" [ngModel]="professional.dateBirth | date:'yyyy-MM-dd'" (ngModelChange)="professional.dateBirth = $event" >

    and I've implemented with datetimepicker() with your solution from here: #2984.

    The error is the same: "ERROR TypeError: this.dateBirth.toISOString is not a function".

    But if I use ngx-bootstrap [http://valor-software.com/ngx-bootstrap/#/datepicker]) it's OK (<ins>but I don't want to use that because i need calendar popup</ins>).

    <div class="form-group form-md-line-input form-md-floating-label no-hint"> <datepicker class="" type="date" name="dateBirth" [ngModel]="professional.dateBirth | date:'yyyy-MM-dd'" (ngModelChange)="professional.dateBirth = $event" required></datepicker> <label>{{l("DateBirth")}}</label> </div> Could you helpme, thks!

  • User Avatar
    0
    jbalda created

    Clarifying:

    • My code with ngx-bottstrap datepicker is: <div class="form-group form-md-line-input form-md-floating-label no-hint"> <datepicker class="" type="date" name="dateBirth" [(ngModel)]="professional.dateBirth" required></datepicker> <label>{{l("DateBirth")}}</label> </div>
    • My project is using Angular 2
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @jbalda,

    Have you tried old answer in this post <a class="postlink" href="http://stackoverflow.com/a/37055451">http://stackoverflow.com/a/37055451</a> ? I was not talking about date pipes.

    Thanks.

  • User Avatar
    0
    KarakTheWise created

    Greetings,

    I'm running into the same issue trying to pass an ngx-datepicker date. I keep getting the "ERROR... 'is not an ISOString(). Is there a soluttion to this yet? I have read the various post above and nothing is getting me there. I can't set my value: this.guestEvent.guestCheckInTime using toISOString() because I then get:

    ERROR TypeError: this.guestEvent.guestCheckInTime.toISOString is not a function
    

    So I'm not sure how to get there from here. This is in the UI, not in the framework btw. Same as the others. I did see from the Git repository that you have switched to 'luxon' from 'moment'. I just purchase ASP.NET Zero about a monthy ago and have no clue to update to a new version at this point :) Any advice would be awesome!

    Thanks

    Update: I found if I submit the 'pure' output from the ngx-datepicker, it will sumit fine. The issue I'm having now is trying to concatinate the ngx-timepicker result on to the date so I end up with somthing like 11/15/2020 16:00. I've been able to get the contatination completed without any problems. It's submitting the result is where I now get the 'toISOString' error once again. Any help would be most appreciated!