Error adding property to object

global variable 
  farray: TJSONArray;

procedure some procedure
var 
  ITem : TJsonObject;
begin
    ITem := farray.Items[0] as TJsonObject;
    if ITem.Values['aaaa'].Value = 'bbbb' then
    begin
        ITem.AddPair('visible', TJSONTrue.Create(true))
    end;
end;

Error Raised on ITem.AddPair('visible'... line
Error is :
Cannot set property 'visible' of undefined | TypeError: Cannot set property 'visible' of undefined at Object.AddPair (http://localhost:8000/ShopAdmin2/WEBLib.JSON.js:226:37) at Object.AddPair$1 (http://localhost:8000/ShopAdmin2/WEBLib.JSON.js:235:12

Earlier it was working fine. Updated to version 1.6.0.1 and 1.6.1.0
thanks

What exact value does this TJSonObject have in the array?
Since i do not have this information, I tested this here with the code:

var
  ITem : TJsonObject;
begin
    ITem := TJsonObject.Create;
    ITem.AddPair('aaaa', 'bbbb');

    if ITem.Values['aaaa'].Value = 'bbbb' then
    begin
        ITem.AddPair('visible', TJSONTrue.Create(true))
    end;
end;

and I could not see an issue here.

Please check this code.
I see a new line is added to the webMemo and then the exception is raised.

procedure TForm2.WebButton1Click(Sender: TObject);
var farray : TJSONArray;
    ITem: TJSONObject;
begin
    farray := TJSONObject.ParseJSONValue('[{"field1":"val1", "field2":1}, {"field1":"val2", "field2":2}]') as TJSONArray;

    ITem := farray.Items[0] as TJsonObject;
    if ITem.Values['field1'].Value = 'val1' then
    begin
      WebMemo1.Lines.Add(ITem.Values['field1'].Value);
     // ITem.AddPair('visible', TJSONTrue.Create(true))
      ITem.AddPair('ffff', TJSONTrue.Create(true))
    end;
end;

procedure TForm2.WebButton2Click(Sender: TObject);
var
    ITem: TJSONObject;
begin
    ITem := TJsonObject.ParseJSONValue('{"field1":"val1", "field2":1}') as TJSONObject;

    if ITem.Values['field1'].Value = 'val1' then
    begin
        WebMemo1.Lines.Add(ITem.Values['field1'].Value);
        //ITem.AddPair('visible', TJSONTrue.Create(true))
        ITem.AddPair('rrrr', TJSONTrue.Create(true))
    end;

end;

We traced & fixed this issue.
The next update will address this.

1 Like