I have an array of objects like with the same setup as the one below. I wanna insert this array of objects into postgres table that looks like this:[![Table setup][1]][1] I have tried to make a function below but it returns error when inserting UNGDOMSKOLE because this is a string so it doesn't understand the space so it crashes on the second input value. How can I make it understand it is a string?
{
'@type': 'SensorSystem',
id: 'SN47230',
name: 'ÅKRA UNGDOMSSKOLE',
shortName: 'Åkra ',
country: 'Norge',
countryCode: 'NO',
geometry: {
'@type': 'Point',
coordinates: [ 5.1963, 59.2555 ],
nearest: false
},
masl: 18,
validFrom: '2013-10-29T00:00:00.000Z',
county: 'ROGALAND',
countyId: 11,
municipality: 'KARM�?Y',
municipalityId: 1149,
stationHolders: [ 'KARM�?Y KOMMUNE' ],
externalIds: [ '506131077' ],
wigosId: '0-578-0-47230'
}
Error code:
error: syntax error at or near "UNGDOMSSKOLE"
What I have tried so far:
let sqlinsert= data.data.map((source)=>{
if (source.geometry) {
if(!source.masl){
source.masl=0
}
let Point = `POINT(${source.geometry.coordinates[0]} ${source.geometry.coordinates[1]})`;
return `(${source.id}, ${source.name}, ${source.shortName},${source.country},${source.countryCode},${source.masl},${source.geometry.coordinates[0]},${source.geometry.coordinates[1]},${Point},${source.validFrom},${source.county},${source.countyId},${source.municipality},${source.municipalityId})`
}
})
const result = await db.query("INSERT INTO sources(source_id,name,shortName,country,countryCode,masl,long,lat,geog,valid_from,county,countyId,municipality,municipalityId) values"+sqlinsert[0])
A second problem I have with this is that inserting
POINT(59.2555 5.1963)
Gives a syntax error at 5.1963 [1]: https://i.sstatic.net/4RSkq.png