Mapping GeoJSON resulting in "TypeError: Cannot convert undefined or null to object" #388
-
I'm trying to create a mapping profile to map from GeoJSON to Proj4GeoJSONFeature, but the mapping fails at the point where the mapper is being created with this error: I'm using the Transformer plugin to decorate my classes, but the mapping is just failing with the above error. I also tried using the decorator, but that is failing since Proj4GeoJSONFeature is a type (and not an interface). DTO: class TestDto {
public id!: number;
public someGeometry!: GeoJSON.GeoJSON;
} ViewModel: import { Proj4GeoJSONFeature } from "proj4leaflet";
class TestViewModel {
public id!: number;
public someGeometry!: Proj4GeoJSONFeature;
} Profile: import { Converter, convertUsing, MappingProfile } from "@automapper/core";
import { Proj4GeoJSONFeature } from "proj4leaflet";
import { TestDto } from "./models.dto";
import { TestViewModel } from "./models.vm";
const testProfile: MappingProfile = (mapper) => {
mapper.createMap(TestDto, TestViewModel)
.forMember(
(destination) => destination.someGeometry,
//mapFrom((source) => source.someGeometry)
convertUsing(geoJsonConverter, (source) => source.someGeometry)
);
};
const geoJsonConverter: Converter<GeoJSON.GeoJSON, Proj4GeoJSONFeature> = {
convert(source) {
debugger;
return { type: "Feature", geometry: { type: "Point", coordinates: [125.6, 10.1] }, properties: [] };
},
}; Example code: // Example data from https://geojson.org/
const geoJson: GeoJSON.GeoJSON = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
};
mapper.map({id:1, someGeometry: geoJson}, TestViewModel, TestDto); One thing to notice is that the I have created a reproduction repository here: https://github.com/PieterBoeren/cra-automapper. I might need some more advanced configuration that the transformer plugin does support, but I really can't figure out what that configuration should be. Any clues? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
I'll take a look as soon as I have some free time. |
Beta Was this translation helpful? Give feedback.
-
In noticed that marking the "geo related" types with the AutoMap decorator with just about anything (Object/Number/Boolean/etc) works. Doesn't feel correct, but at least I'm getting into the custom type converter (example shows Object, but Boolean works just as fine). import { AutoMap } from "@automapper/classes";
import { Proj4GeoJSONFeature } from "proj4leaflet";
class TestViewModel {
public id!: number;
@AutoMap({ typeFn: () => Object })
public someGeometry!: Proj4GeoJSONFeature;
}
export { TestViewModel }; |
Beta Was this translation helpful? Give feedback.
-
Since I can only select my own answer above as answer to the entire discussion (which contains code which isn't longer necessary): the issue has been fixed in version 7.1.1. Thanks @nartc! |
Beta Was this translation helpful? Give feedback.
Since I can only select my own answer above as answer to the entire discussion (which contains code which isn't longer necessary): the issue has been fixed in version 7.1.1. Thanks @nartc!