I,m using postgreSQL 9.1 , postGIS 2.0, nodejs 0.10.12 and the latest version of node's pg module.
I use websockets on the server side. I have two functions. Depending on the data came from the user I call the right function.
First function is a simple SELECT, works fine.
The second tries to find 5 nearest neighbors of a given geometry (it can be point or line or polygon). Does not work. I tried different syntax for the nearest neighbor query and still nothing.
What could be wrong? The query? I was thinking maybe the pg module does not support the ST_DWithin function.
Here is the code of the nearest neighbor function
function checkMapIn(je){
var conString = "pg://username:password@localhost:5432/myDB";
var client = new pg.Client(conString);
client.connect();
//je came from client, is a geometry he just inserted in the map
var query = client.query('SELECT pins.p_name FROM pins ORDER BY pins.p_geom <-> '+je+' LIMIT 5')
//alternative syntax I tried
//SELECT pins.p_name FROM pins INNER JOIN pins ON ST_DWithin('+je+', pins.p_geom, 1000) LIMIT 5
//SELECT pins.p_name FROM pins WHERE ST_DWithin('+je+', pins.p_geom, 1000) LIMIT 5
query.on("row", function (row, result) {
result.addRow(row);});
query.on("end", function (result) {
console.log(JSON.stringify(result.rows, null, " "));
for (var i=0; i<result.rows.length; i++){
connection.send(result.rows[i].p_name+'</br>')
}
client.end();
});
}
And this is the error I get , no matter how I edit the synatx of the query
events.js:72
throw er; // Unhandled 'error' event
^
error: syntax error at or near "["
at Connection.parsE (C:\Program Files (x86)\nodejs\node_modules\pg\lib\connection.js:526:11)
at Connection.parseMessage (C:\Program Files (x86)\nodejs\node_modules\pg\lib\connection.js:371:17)
at Socket.(anonymous) (C:\Program Files (x86)\nodejs\node_modules\pg\lib\connection.js:86:20)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.(anonymous) (_stream_readable.js:736:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:156:9)
at Spcket.Readable.push (_stream_readable.js:127:10)
Any advise? Hints?
Thanks in advance
slevin
EDIT
Here is an example value of the 'je' variable, as "badsyntax" asked.
[object Object]
I get this if I set connection.send('</br>'+je+'</br>').
Also, this is what I get from openlayers in the client side and what I send to the server.
POINT(2332239.3475 4729773.7440625)
Weird...
On the client side I use openlayers to get the geometry of the feature just inserted on the map.
In cases like this, your first step is to separate where your problem is.
Is your query one that works from psql? If not, you may want to ask here, on gis, or on dba.
If your query does work in psql, then your issue is in nodejs. You will want to take a close look and create another question probably here or follow up on email lists with the developers or the like.
Your error looks like it is being thrown by Node.js but it is hard to tell if this is a bug there (in which case email lists are best), or whether it is in your sql. I can't tell what is being passed into your function.
As a side note, you need to find a way to sanitize je. Otherwise you are asking for SQL injection issues.