Reputation: 3886
I am biulting a register system. I use node, express and my database is PostgreSQL. I use the latest pg module to communicate with Postgre.
My user-getting function is multipurpose, gets a user based on its id or its username. (back-end)
User.get = function (info,t, fn){
var text;
if(t=="id"){
text = "SELECT user.id, user.usrnm FROM user WHERE user.id=$1";
}
else{
text = "SELECT user.id, user.usrnm, user.role FROM user WHERE user.usrnm=$1";
}
var query = client.query({ text:text, values:[info]}, function(err, result){
if(err) {return fn(err);}
query.on("row", function (row, result) {
result.addRow(row);
});
if(result.rows.length==0){//no user found
return fn(err, null);
query.on("end", function(){client.end();});
}
if(t=="id"){
return fn(null, new User({
id:result.rows[0].id,
usrnm:result.rows[0].usrnm
})
);
query.on("end", function(){clientGuest.end();});
}
else{
return fn(null, new User({
id:result.rows[0].id,
usrnm:result.rows[0].usrnm,
role:result.rows[0].role
})
);
query.on("end", function(){clientGuest.end();});
}
});
query.on("end", function(){clientGuest.end();});
}
So, I use the above to check if there is a user already using a username. If not, I save the new user (register.) (front-end)
User.get(username, "name", function(error, user){
if(error) return next(err);
if(user!=null){//so, we found a user
res.error("Username already in use");
res.redirect('back');
res.end();
}
else{
user = new User ({
usrnm:username,
pswrd:userpass
});
user.save(function(err){
if(err) return next(err);
req.session.uid=user.id;
})
res.redirect('/');
res.end();
}
})
});
Whether I insert a unique username or one that is already used, the first time all works well, The second time, the data are sent to the User.get
, and after that the exacution hangs.
I put several console.log
inside the query and only one gives data back. The one right after User.get = function (info,t, fn){
that show the value of t
and info
. All the other console.log
show nothing.
I dont know how to fix this. Maybe its a detail I dont get. Plaese help.
Thanks
Upvotes: 1
Views: 114
Reputation: 3886
I changed my user get function to this
User.get = function (info,t, fn){
var text;
if(t=="id"){
text = "SELECT user.id, user.usrnm FROM user WHERE user.id=$1";
}
else{
text = "SELECT user.id, user.usrnm, user.role FROM user WHERE user.usrnm=$1";
}
var query = client.query({ text:text, values:[info]}, function(err, result){ if(err) {return fn(err);} });
query.on("row", function (row, result) {
result.addRow(row);
});
query.on("end", function(result){
if(result.rows.length==0){return fn(null);}
if(t=="id"){
return fn( new User({
id:result.rows[0].id,
usrnm:result.rows[0].usrnm
})
);
}
else{
return fn( new User({
id:result.rows[0].id,
usrnm:result.rows[0].usrnm,
role:result.rows[0].role
})
);
}
client.end();
});
}
So query.on row
is simple and adds rows. When we are sure that the query is finished, query.on("end"
takes action and counts the rows, deciding what to return.
I guess my problem was that the checking or rows and the t
value was outside the query.on end
, so it is possible that data were send before the query was finished or the opposite, the query finished before any data send, depending on the time the query needed to search for data, bring them back and add them to the result
.
Upvotes: 1