(JSON) Object parser Browser vs. Client
I am despairing of what I would call a bug.
I have a function (global) that works fine in the browser, but fails in the app and breaks the entire global script.
The problem is to store a JSON object in a list, here to produce a valid GeoJSON with.
features: [{...}]
If I do not enforce this list (but therefore no valid GeoJSON), it is not a problem.
The error indicates that an object is being written to that does not exist.
Here are two attempts, both unsuccessful (in CLI):
Version 1
function make_GeoJSON(ref : Places,JJ : text) do
let fJ := [{}];
let gJ := {};
let pJ := {
PLACE_ID: number(ref),
PLACE_NAME: ref.Name,
PLACE_TYPE: ref.Type.Name,
PLACE_TYPE_ID: ref.Type.number(Id),
distance: 0
};
if JJ = "" then JJ := ref.GeoJSON end;
let J := parseJSON(JJ);
if cnt(J.features) > 0 then
fJ := [J.first(features)];
if ref.'Primary Location' and longitude(ref.'Primary Location') != 0 and
latitude(ref.'Primary Location') != 0 and
longitude(ref.'Primary Location') and
latitude(ref.'Primary Location') and
first(fJ).geometry.type = "Point" then
setItem(first(fJ).geometry, "coordinates", [longitude(ref.'Primary Location'), latitude(ref.'Primary Location')])
end;
if not first(fJ).properties then
setItem(first(fJ), "properties", pJ)
end;
if not first(fJ).properties.distance then
setItem(first(fJ).properties, "distance", 0)
end;
setItem(first(fJ), "id", number(ref));
setItem(first(fJ).properties, "PLACE_ID", number(ref));
setItem(first(fJ).properties, "PLACE_NAME", ref.Name);
setItem(first(fJ).properties, "PLACE_TYPE", ref.Type.Name);
setItem(first(fJ).properties, "PLACE_TYPE_ID", ref.Type.number(Id));
setItem(J, "features", fJ)
else
void
end;
formatJSON(J)
end;
Here is the problem in:
let fJ := [{}];
Version 2
function make_GeoJSON(ref : Places,JJ : text) do
let fJ := {};
let gJ := {};
let pJ := {
PLACE_ID: number(ref),
PLACE_NAME: ref.Name,
PLACE_TYPE: ref.Type.Name,
PLACE_TYPE_ID: ref.Type.number(Id),
distance: 0
};
if JJ = "" then JJ := ref.GeoJSON end;
let J := parseJSON(JJ);
if cnt(J.features) > 0 then
fJ := J.first(features);
if ref.'Primary Location' and longitude(ref.'Primary Location') != 0 and
latitude(ref.'Primary Location') != 0 and
longitude(ref.'Primary Location') and
latitude(ref.'Primary Location') and
fJ.geometry.type = "Point" then
setItem(fJ.geometry, "coordinates", [longitude(ref.'Primary Location'), latitude(ref.'Primary Location')])
end;
if not fJ.properties.distance then
setItem(fJ.properties, "distance", 0)
end;
setItem(fJ, "id", number(ref));
setItem(fJ.properties, "PLACE_ID", number(ref));
setItem(fJ.properties, "PLACE_NAME", ref.Name);
setItem(fJ.properties, "PLACE_TYPE", ref.Type.Name);
setItem(fJ.properties, "PLACE_TYPE_ID", ref.Type.number(Id));
setItem(J, "features", [fJ])
else
void
end;
formatJSON(J)
end;
Here the problem is in
setItem(J, "features", [fJ])
To make it easier to understand, here is a correct GeoJSON:
{"type":"FeatureCollection","features":[{"geometry":{"coordinates":[-1.4440324,40.405312],"type":"Point"},"id":28,"properties":{"PLACE_ID":28,"PLACE_NAME":"Albarracín","PLACE_TYPE":"locality","PLACE_TYPE_ID":55,"distance":1180.7024668824363},"type":"Feature"}]}
PS: And the void in else is not in the original, here only for shortening. In fact, an entire GeoJSON is constructed there instead of being updated.
ref is a record from table Places. Behavior is the same if the script is nested in a local formula.
4 replies
-
A minimal database for testing attached. It works in the Browser and still produces the error in the client app
-
Problem solved, an update or a small adjustment in the script did the trick, see https://forum.ninox.de/t/m1ym0fq#60ymaq4
Content aside
- Status Answered
- 2 mths agoLast active
- 4Replies
- 68Views
-
2
Following