I'm using websockets and nodejs 0.10.12 and node-postgre 2.2.0 to send data from server to client.
Server gets an id of an object clicked on the client, and performs a query, based on that id (is an int).
I want to get some textual data for this object, along with the names of some images. Just the names (image01.jpg), not the files.
The structure of the database is like so : There are two tables, pins
(the objects) and multi
(stands for multimedia). There is a column in multi
, named m_pins
which represents the id of a pins
. So I can connect what multi belongs to which pins
. So multi
2,3,4,5,6 belong to pins
2, if they have 2 in m_pins
.
With those facts given, please check the code
client.connect();var query = client.query('SELECT pins.p_name, type.t_name, era.e_name, controller.c_name, multi.m_name FROM pins,era, type, controller, multi WHERE type.t_id=pins.p_type AND era.e_id=pins.p_era AND controller.c_id=pins.p_controller AND multi.m_pins =pins.p_id AND pins.p_id ='+eoid)//eoid came from client
query.on("row", function (row, result) {result.addRow(row);});
query.on("end", function (result) {
//get all the images, now that query comleted
var mnm=[];
for (var i=0; i<result.rows.length; i++)
{mnm.push(result.rows[i].m_name);}
//get other data...we dont need the "for" now, its the first element of every table, since its about just one object
var name = result.rows[0].p_name;
var ty = result.rows[0].t_name;
var er = result.rows[0].e_name;
var cn = result.rows[0].c_name;
//send to user
connection.send(JSON.stringify({name:name,ty:ty, er:er, cn:cn,mnm:mnm}));
client.end();
});
Looks normal to me. But does not work. In the console I get Cannot read property 'p_name' of undifined
. Its about this line var name = result.rows[0].p_name;
The weird thing is that if I remove everyhting about the multi
table, works fine.
I tried many alternative syntaxes, but cannot fix it.
Thanks for your patience