Consuming Xdata sql generated

I have created the interface as shown in your youtube video and it works just fine. I am able to retrieve the data from the url 


    • Sampledate"20200121T000000"
    },
  • {
    • Sampledate"20200117T000000"
    },
But data does not have a data node. I am able to call the function 'IRunQueryService.GetQueryData', [1,'','','','']) and the network tab in the chrome browsers shows the query correctly. The response also comes in correctly But I am not able to use it with 

  XDataWebDataset1.SetJsonData(TJSObject(Response.Result));
//  XDataWebDataset1.SetJsonData(TJSObject(Response.Result)['value']);

How do I get this data into the dataset ?

What is the exact declaration of the interface method in XData server, and what is the exact JSON you received from the server, according to the browser network tab?

unit uRunQueryService;

interface

uses
  XData.Server.Module,
  XData.Service.Common, System.Classes;

type
  [ServiceContract]
  IRunQueryService = interface(IInvokable)
    ['{F18EA5B4-E27C-46FD-BDA2-0EF817EE3738}']

    [HttpGet]
    function GetQueryData(sqlno:integer;param1,param2,param3,param4:string):TStream;
  end;

  [ServiceImplementation]
  TRunQueryService = class(TInterfacedObject, IRunQueryService)
    function GetQueryData(sqlno:integer;param1,param2,param3,param4:string):TStream;
  end;

implementation


{ TRunQueryService }

uses UQuery;

function TRunQueryService.GetQueryData(sqlno: integer;param1,param2,param3,param4:string): TStream;
var LResult:TMemoryStream;
begin
  LResult := TMemoryStream.Create;
  DmQuery.GetQueryData(sqlno,param1,param2,param3,param4,LResult);
  Result := LResult;
end;
-----------------------------------

procedure TdmQuery.GetQueryData(sqlno:integer;param1,param2,param3,param4:string;AStream: TStream);
var
  sql: string;

begin

{  Without batchmove for firedac
  if assigned(AStream) then
  begin
    qry.Close;
    if sqlno=1 then
    begin

      sql := 'select distinct Sampledate from trans order by Sampledate desc';
      if param1 <> '' then
      sql := 'select distinct Customer_id from trans Where Sampledate='+quotedstr(param1)

    end

    else if sqlno=2 then
    sql := 'Select * from Usermaster'  ;

    qry.Open(sql);
    qry.SaveToStream(AStream, sfJSON);
  end;

}

{
  With batchmove }
  if assigned(AStream) then
  begin

    qry.Close;
    if sqlno=1 then
    begin

      sql := 'select distinct Sampledate from trans order by Sampledate desc';
      if param1 <> '' then
      sql := 'select distinct Customer_id from trans Where Sampledate='+quotedstr(param1)

    end

    else if sqlno=2 then
    sql := 'Select * from Usermaster'  ;

    qry.Open(sql);
    BMJasonWriter.Stream := AStream;
    FDBM.Execute;
  end;



end;


Url
http://localhost:2001/tms/xdata/RunQueryService/GetQueryData/?sqlno=1&param1=%27%27&param2=%27%27&param3=%27%27&param4=%27%27

data that I get 
[
  • {
    • Sampledate"20200124T000000"
    },
  • {
    • Sampledate"20200121T000000"
    },
  • {
    • Sampledate"20200117T000000"
    },
  • {
    • Sampledate"20200116T000000"
    },

The JSON you get is an array, so I guess you should use


  XDataWebDataset1.SetJsonData(TJSArray(Response.Result));

But since you are returning a TStream, I guess you should use

  XDataWebDataset1.SetJsonData(TJSArray(TJSJson.Parse(JS.ToString(Response.Result))));

Thanks. This worked. I don't think I would have been able to figure this out on my own. Since you have JS.Tostring, everything gets converted to a string. In case I need to get a Jason object instead of just an array the way you use in your examples what needs to be done ?

Secondly which method is better in this case. Since in webcore we are not using firedac I thought the batchmove method is better. Please advice

Do you see a problem with this issue? I think its ok.

No This woks  Since you have JS.Tostring, everything gets converted to a string. In case I need to get a Jason object instead of just an array the way you use in your examples can you please tell me what needs to be done 

That's just the XDatawebClient that gets the TStream result as a string. If you perform a request using raw TWebHttpRequest, or return a TJSONArray instead of a TStream, then TXDataWebClient will treat it as a regular JSON object.

But this string behavior is specific to how TXDataWebClient treats TStream results from XData.

My current issue is resolved and I can go ahead with my project based on what you have already advised. I will have to work on this issue once I am free.