#1 2025-08-21 17:58:51

DidiL
Member
Registered: 2015-01-06
Posts: 14

Retrieve JSON value

Can I be corrected for this code ?
I definitely miss Mormot2 documentation

function TUtilisateursService.GetUtilisateur(aID: TID): RawJSON;
var
  obj: TOrmUtilisateurs;
begin
  if not fClientDB.Retrieve(aID, obj, false) then
  begin
    raise ERestException.Create('404 - Utilisateur non trouvé');
  end;
  try
    Result := JsonEncode(obj);
  finally
    obj.Free;
  end;
end;

Offline

#2 2025-08-21 20:54:07

DidiL
Member
Registered: 2015-01-06
Posts: 14

Re: Retrieve JSON value

[dcc32 Erreur] Utilisateurs.pas(66): E2250 Aucune version surchargée de 'JsonEncode' ne peut être appelée avec ces arguments

Offline

#3 2025-08-21 21:19:37

flydev
Member
From: France
Registered: 2020-11-27
Posts: 112
Website

Re: Retrieve JSON value

You are missing the object creation that will trigger an AV and use ObjectToJson, not JsonEncode.

function TUtilisateursService.GetUtilisateur(aID: TID): RawJSON;
var
  obj: TOrmUtilisateurs;
begin
  obj := TOrmUtilisateurs; // <- missing create
  try
    if not fClientDB.Orm.Retrieve(aID, obj) then 
      raise ERestException.Create('404 - Utilisateur non trouvé');

    Result := ObjectToJson(obj); 
  finally
    obj.Free;
  end;
end;

ou

function TUtilisateursService.GetUtilisateur(aID: TID): RawJSON;
var
  obj: TOrmUtilisateurs;
begin
  obj := TOrmUtilisateurs.Create(fClientDB.Orm, aID);
  try
    if ID = 0 then
      raise ERestException.Create('404 - Utilisateur non trouvé'); 

    Result := ObjectToJson(obj); 
  finally
    obj.Free;
  end;
end;

read there: SAD1.18 5.2. Working with Objects

Offline

#4 2025-08-21 21:48:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,153
Website

Re: Retrieve JSON value

You are still working at HTTP level in your mind: 404 error, bulk JSON result...
Just define services, with high level code, as if it was running locally.
So here you could e.g. return a TOrmUtilisateurs in an "out" parameter, with a boolean for success/failure.

Offline

#5 2025-08-21 22:13:05

DidiL
Member
Registered: 2015-01-06
Posts: 14

Re: Retrieve JSON value

flydev wrote:

You are missing the object creation that will trigger an AV and use ObjectToJson, not JsonEncode.
read there: SAD1.18 5.2. Working with Objects

Very helpfull
Thanks!

Offline

#6 2025-08-21 22:16:49

DidiL
Member
Registered: 2015-01-06
Posts: 14

Re: Retrieve JSON value

ab wrote:

You are still working at HTTP level in your mind: 404 error, bulk JSON result...
Just define services, with high level code, as if it was running locally.
So here you could e.g. return a TOrmUtilisateurs in an "out" parameter, with a boolean for success/failure.

Right, it should be more informative.
Thank you for the advice.

Offline

Board footer

Powered by FluxBB