Recent

Author Topic: Why this.edtEmail is undefined?  (Read 2170 times)

cappe

  • Full Member
  • ***
  • Posts: 191
Why this.edtEmail is undefined?
« on: November 07, 2022, 03:55:26 pm »
Why with this code:

{$mode objfpc}

uses
  browserapp, JS, Classes, SysUtils, Web, fpjson, fpjsonjs, fprpccodegen;

type

  { TMyApplication }

  TMyApplication = class(TBrowserApplication)
    edtEmail: TJSHTMLInputElement;
    edtPassword : TJSHTMLInputElement;
    btnLogin : TJSHTMLButtonElement;
    procedure doRun; override;
  private
    procedure BindElements;
    procedure doLoginClick(aEvent: TJSEvent);
    procedure doServerLogin(const aUser, aPassword: String);
  end;

procedure TMyApplication.doRun;

begin
  BindElements;
  btnLogin.AddEventListener('click',@DoLoginCLick);
  Terminate;
end;

procedure TMyApplication.BindElements;
begin
  edtEmail:=TJSHTMLInputElement(GetHTMLElement('edtEmaill'));
  edtPassword:=TJSHTMLInputElement(GetHTMLElement('edtPassword'));
  btnLogin:=TJSHTMLButtonElement(GetHTMLElement('btnLogin'));
end;

procedure TMyApplication.doLoginClick(aEvent: TJSEvent);
begin
  window.alert(edtEmail.value);
  DoServerLogin(edtEmail.value,edtPassword.value);
end;

procedure TMyApplication.doServerLogin(const aUser, aPassword: String);
begin
  window.alert('Logging in with user: '+aUser);
  // to be implemented.
end;


Why does doLoginClick give me the following error? A seems correct.

Uncaught TypeError: this.edtEmail is undefined
    doLoginClick client.lpr:50
    cb rtl.js:256
    DoRun client.lpr:29
    Run custapp.pas:206
    $main client.lpr:66
    run rtl.js:141

« Last Edit: November 07, 2022, 04:07:07 pm by cappe »

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Why this.edtEmail is undefined?
« Reply #1 on: November 07, 2022, 04:04:21 pm »
Code: Pascal  [Select][+][-]
  1. 'edtEmaill'

It has double L in the end

cappe

  • Full Member
  • ***
  • Posts: 191
Re: Why this.edtEmail is undefined?
« Reply #2 on: November 07, 2022, 04:09:44 pm »
Code: Pascal  [Select][+][-]
  1. 'edtEmaill'

It has double L in the end

That's not the problem, I have attached the project, I try with pas2js 2.2.0 I can't get it to work.

The strange thing is that inside the BindElements works, edtEmail works correctly, while in doLoginClick it gives me "Uncaught TypeError: this.edtEmail is undefined"
« Last Edit: November 07, 2022, 04:15:21 pm by cappe »

440bx

  • Hero Member
  • *****
  • Posts: 4063
Re: Why this.edtEmail is undefined?
« Reply #3 on: November 07, 2022, 11:22:27 pm »
@cappe,

your opening post would be much easier to read if you used code tags to format the code. ;)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PierceNg

  • Sr. Member
  • ****
  • Posts: 374
    • SamadhiWeb
Re: Why this.edtEmail is undefined?
« Reply #4 on: November 08, 2022, 03:53:18 am »
That's not the problem, I have attached the project, I try with pas2js 2.2.0 I can't get it to work.

The strange thing is that inside the BindElements works, edtEmail works correctly, while in doLoginClick it gives me "Uncaught TypeError: this.edtEmail is undefined"

"It works on my computer"TM with pas2js 2.2.1. The login box is displayed, the alert modal pops up, and browser console shows no relevant error.

yus

  • Jr. Member
  • **
  • Posts: 57
Re: Why this.edtEmail is undefined?
« Reply #5 on: November 09, 2022, 03:17:52 am »
Remove Application.Free;
Code: Pascal  [Select][+][-]
  1. ...
  2. var
  3.   Application : TMyApplication;
  4.  
  5. begin
  6.   Application:=TMyApplication.Create(nil);
  7.   Application.Initialize;
  8.   Application.Run;
  9. //  Application.Free;
  10. end.

 

TinyPortal © 2005-2018