Recent

Author Topic: client doesn't show data  (Read 8031 times)

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: client doesn't show data
« Reply #15 on: July 01, 2020, 05:12:15 pm »
OK, i reinstated the id created as intended. JSon data now looking good
Code: Pascal  [Select][+][-]
  1. Procedure LoadRestData;
  2. Var
  3.   F : TFileStream; D : TJSONData; I : Integer;
  4. begin
  5.   F:=TFileStream.Create('fust.json',fmOpenRead or fmShareDenyWrite);
  6.   try
  7.     D:=GetJSON(F);
  8.     if not (D is TJSONObject)
  9.       then D.Free
  10.     else Packet:=D as TJSONObject;
  11.     // add new field "id" for referencing individual rows
  12.     MetaData.Arrays['fields'].Add(TJSONObject.Create(['name','id','type','int']));
  13.     // 'fix' the (original json) data to include a new field named "id", and give it a (unique) number
  14.     For I:=0 to Data.Count-1 do
  15.       Data.Objects[i].Add('id',I+1);
  16.     LastID:=Data.Count;    
  17.   finally
  18.     F.Free;
  19.   end;
  20. end;
  21.  

Then the console thows us a:
Code: [Select]
myclient1.js:12607 Uncaught TypeError: Cannot read property 'append' of null
at Object.DSOpen (myclient1.js:12607)
at Object.cb [as FAfterOpen] (myclient1.js:217)
at Object.DoAfterOpen (myclient1.js:6823)
at Object.OpenCursorcomplete (myclient1.js:6946)
at Object.SetActive (myclient1.js:6973)
at Object.Open (myclient1.js:7439)
at Object.HandleRequestresponse (myclient1.js:6571)
at Object.cb [as FAfterRequest] (myclient1.js:217)
at Object.DoAfterRequest (myclient1.js:7774)
at Object.onLoad (myclient1.js:12353)
That's enough for me today  :D

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: client doesn't show data
« Reply #16 on: July 02, 2020, 03:05:44 pm »
Thank you very very very much, TRon.

I have finally results on screen.

The problem is documentation. Offcourse you can learn a lot of examples, but comments are very usefull why this is happening.
Quote
in the restserver example, it seems that the original (stored on disk) data is extended (before sending to the client) with an extra field with the name "id" of type "int". That is why the restserver example seems to be 'fixing' all the data by adding an id field using the following loop....
.
I looked in the original file and saw this line
Code: Pascal  [Select][+][-]
  1. MetaData.Arrays['fields'].Add(TJSONObject.Create(['name','id','type','int']));
as the same fielddefs in countries.json. That's why I tought the same meta fields in my json file must be exactly. That is the reason I changed it. There was no doc to tell that it should always be as the above code line.

I Put the lines of pascaldragon into the restserver to tell the CORS that the streaming is valid. But still no results.
You option to set the header
Code: Pascal  [Select][+][-]
  1. res.SetFieldByName('Access-Control-Allow-Origin', '*');
is for this moment the only way to get result.
Hopefully there's a 'clean' way to do it otherwise.

 
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: client doesn't show data
« Reply #17 on: July 02, 2020, 04:09:38 pm »
Thank you very very very much, TRon.
You are more than welcome mangakissa.

I had/have fun with learning about a fpc topic that i've never touched before.

Quote
I have finally results on screen.
Cool. Congratulations ... and I have so many questions  :)

For example do you filter the data on the server or on the client ? From your code that I've seen so far it was not clear to me what you exactly intended to do there.

Quote
The problem is documentation. Offcourse you can learn a lot of examples, but comments are very usefull why this is happening.
Yes, I agree.

Having an example is one thing but figuring out how all these new classes work is daunting enough let alone example-code that seems to do something out of the ordinary. At least a small comment about what or why it was done could have been of tremendous help there.

Of course, in hindsight, it is now obvious (as is usually the case with these kind of things)  :P

Quote
Hopefully there's a 'clean' way to do it otherwise.
Yeah, I tried to delve into that subject today but so far and unfortunately turned up empty :-(

Even though you already know, I do again emphasis on the risks involved using that header. Do not use that for production, and in case you must then restrict it to domains you are able to trust.

Will you be doing an article on this subject as well (not trying to force your hand there) ? afaik there is not an example that uses search queries on a json rest dataset, therefore I would be interested. Some of this stuff is still going a bit over my head I'm afraid.
« Last Edit: July 02, 2020, 04:23:26 pm by TRon »

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: client doesn't show data
« Reply #18 on: July 02, 2020, 05:03:09 pm »
Quote
Cool. Congratulations ... and I have so many questions  :)

For example do you filter the data on the server or on the client ? From your code that I've seen so far it was not clear to me what you exactly intended to do there.
It's a list with how many packages and which packages a supplier need to fill flowerbulbs in. The real data is a spreadsheet. But spreadsheets are nog multiple users. the second user is read only. If the 'main' colleague want to change it hwen another colleague has opened it, changes can not be made.
In Lazarus I built an application that reads the spreadsheet once. With selections I can filter the data. Every time I open that application, new data is read. Also an button can refresh the data.

It could also works with a webbrowser and javascript. That's why I try to use pas2js.

Filtering must be done by server. The client must be a stupid client. Only show data when it is requested. I want the client run on a smartphone.
Quote
Will you be doing an article on this subject as well ? afaik there is not an example that uses search queries on a json rest dataset, therefore I would be interested. Some of this stuff is still going a bit over my head I'm afraid.
i always do that with these projects
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: client doesn't show data
« Reply #19 on: July 02, 2020, 07:05:55 pm »
Thank you for the explanation of your project mangakissa, sounds interesting.

Filtering must be done by server. The client must be a stupid client. Only show data when it is requested. I want the client run on a smartphone.
Ok, I suspected as such (and it made the most logical sense).

Client runs on a smartphone ? Does that mean you are able to use the pas2js generated code in (what I assume) Android directly ?

Nice if you are able to cook up some article on your findings with regards to pas2js and using json rest db. I'll be looking forward to it, I thank you in advance.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: client doesn't show data
« Reply #20 on: July 02, 2020, 07:55:20 pm »
Quote
Client runs on a smartphone ? Does that mean you are able to use the pas2js generated code in (what I assume) Android directly ?
No. Just using the browser calling a webadress
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: client doesn't show data
« Reply #21 on: July 02, 2020, 11:52:24 pm »
@mangakissa:
uhm, of course  :-[ That's a facepalm for me

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: client doesn't show data
« Reply #22 on: July 03, 2020, 09:12:55 am »
@mangakissa:
uhm, of course  :-[ That's a facepalm for me

Though one can also host the code in an app that allows the running of JS code (for example Apache Cordova).

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: client doesn't show data
« Reply #23 on: July 15, 2020, 03:19:28 pm »
Small update.
Close the dataset after loading / showing data. If it's still open, data will not refresh.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018