I’m creating a walking path tracking app using google maps api, geolocation, and Postgresql for the database.
To draw a polyline on a map, it gets an array of coordinate sets and draws line between them.
The coordinates data looks like this:
const polylineCoordinates = [
{ lat: 37.7749, lng: -122.4194 },
{ lat: 37.8044, lng: -122.2711 },
{ lat: 37.6879, lng: -122.4702 },
{ lat: 37.6391, lng: -122.4000 },
{ lat: 37.5585, lng: -122.2711 },
{ lat: 37.4419, lng: -122.1430 },
{ lat: 37.7749, lng: -122.4194 }
];
And this is the ‘route’ schema that contians an array of coordinates:
CREATE TABLE routes(
id SERIAL PRIMARY KEY,
coords geometry(POINT, 4326)[],
distance float,
steps integer,
time_taken INTERVAL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE
)
The post request query in backend:
const createNewRoute = 'INSERT INTO routes (coords, distance, steps, user_id) VALUES ($1, $2, $3, $4)';
The post request controller in backend:
const createNewRoute = (req, res) => {
const { coords, distance, steps, user_id } = req.body;
pool.query(queries.createNewRoute, [coords, distance, steps, user_id],(error, results) => {
if(error) throw error;
const newRoute = {
coords: coords,
distance: distance,
steps: steps,
user_id: user_id
}
res.status(201).json({ newRoute: newRoute });
});
}
Is this a correct way to save sets of coordinates?
When I send a post request with this data, I get a parse error from psql, saying it’s an unexpected array element.