TXDataWebConnection and Await

Further to my question TXDataWebConnection.SendRequest and Await - BIZ / TMS XData - TMS Support Center (tmssoftware.com).

I think I must have missed something here as I am not getting Await to work (I don't run both methods at the same time):

  Conn := TXDataWebConnection.Create(nil);
  try
    //method 1:
    Conn.SendRequest(THttpRequest.Create('config/config.json'), @OnSuccess, @OnError);

    //Method 2:
    Response := Await(IHttpResponse, Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));
    if Response.StatusCode = 200 then
       Config.Load(Response.ContentAsText);
  finally
    Conn.Free;
  end;

Method 1 works, but Method 2 results in

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status')

What have I missed?

You syntax is strange:

   Response := Await(IHttpResponse, Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));

It should be just:

   Response := Await(Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));

Maybe you tried to use the Web-Core specific type-safe approach as described here? In this case, it should be this way:

   Response := TAwait.Exec<IHttpResponse>(Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));

I used that syntax because I think every example in the TMSWEBCoreDevGuide.pdf uses the format

Await(<ReturnType>, Command);

I have tried the alternatives that you suggest:

Response := Await(Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));

This returned the same exception

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status')

The second alternative:

Response := TAwait.Exec<IHttpResponse>(Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));

When compiled showed as

Response := Await(Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));

when examining the source code in the browser:

In Delphi:

In Browser
In browser

So also resulted in the same exception:

Can you maybe please send us a small sample project reproducing the issue? It should be simple to reproduce it by requesting any public URL from the internet, isn't it?

Here is a test project. It illustrates

  1. the conversion of Await.Exec to Await
  2. the success on the use Conn.SendRequest(THttpRequest.Create('config/config.json'), @OnSuccess, @OnError);
  3. the error in using Response := Await(Conn.SendRequestAsync(THttpRequest.Create('config/config.json')));

WebCoreDemo.zip (1006.2 KB)

Sorry, but your project is absolutely empty. There are two data modules in there with no components or code in them.

No idea what happened there. Try this one.
Demo.zip (1004.1 KB)

Thank you. I see now the issue. You can fix it directly in your code, in unit <xdata>\source\core\web\XData.Web.Connection.pas, method TXDataWebConnection.SendRequestAsync, around line 500, please add the following line:

        procedure(Response: IHttpResponse)
        begin
          Response._AddRef; // ADD THIS LINE
          resolve(Response);
        end,

The fix will be included in next release.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.