r/coffeescript • u/hego555 • Jun 23 '14
Implicit Return Messed With Me
TL;DR Returned my SQL Object
So I made a function to check if the user is logged in, and I was getting this odd error "TypeError: Converting circular structure to JSON at Object.stringify"
Having no idea what to do I read, and re-read my code trying to understand what is wrong.
After a while I decided it be easier to read it in pure JS, so I complied it and looked over it, nothing stood out. SO I decided to see what the function was returning.
It was a returning a huge JSON object, I was confused because the function is supposed to return only true or false.
I looked for extra returns and didn't see any, then I checked the Javascript and THERE IT WAS
checkLoggedIn = (ID, request)->
console.log("CheckLoggedin")
console.log "#{request.session.username}:#{request.session.password}:#{request.session.ID}"
if request.session.username? and request.session.password? and request.session.ID?
console.log("B")
connection.query "SELECT Username,Password FROM `Users` WHERE `ID` = '#{ID}'", (err, rows, fields) ->
if err?
console.log("ERROR?!")
console.log("Err: "+err)
return false;
if !rows[0]?
console.log(rows[0])
console.log("Not logged In?");
return false;
else
if rows[0]==request.session.username and rows[1]==request.session.password
console.log(request.session.username+" still logged in")
return true;
else
return false;Can you find the problem?
How about now?
var checkLoggedIn;
checkLoggedIn = function(ID, request) {
console.log("CheckLoggedin");
console.log("" + request.session.username + ":" + request.session.password + ":" + request.session.ID);
if ((request.session.username != null) && (request.session.password != null) && (request.session.ID != null)) {
console.log("B");
return connection.query("SELECT Username,Password FROM `Users` WHERE `ID` = '" + ID + "'", function(err, rows, fields) {
if (err != null) {
console.log("ERROR?!");
console.log("Err: " + err);
return false;
}
if (rows[0] == null) {
console.log(rows[0]);
console.log("Not logged In?");
return false;
} else {
if (rows[0] === request.session.username && rows[1] === request.session.password) {
console.log(request.session.username + " still logged in");
return true;
}
}
});
} else {
return false;
}
};If you still haven't seen it I'll point it out. return connection.query("SELECT Username,Password FROM Users WHERE ID = '" + ID + "'", function(err, rows, fields) {
The implicit return was the issue, the solution was to add a return false at the end of the code -.-
</rant>
6
u/DavetheBassGuy Jun 24 '14
The problem isn't really to do with implicit with, it is because the SQL query is asynchronous. You're solution of adding
return falseafter the query means the function will always return false! Even if the user is logged in. You can't return a value from an async callback function. You need the caller to pass in another callback function which you call to give it the result.2
u/DavidBonnet Jun 24 '14 edited Jun 24 '14
I agree with DavetheBassGuy, the
checkLoggedInfunction does not work as intended. Yet, this raises an interesting question about implicit returns in CoffeeScript.I really wish implicit returns were only applied for single line functions such as:
It would have been better if functions with a carriage return right after the arrow would require the
returnkeyword. E.g., this would returntrue:And this would return nothing (
undefined):Returning a value has little to no cost, except for list comprehensions such as:
In the above case, it builds a costly results array. Having to write an extra
returnstatement makes it look as unnecessary as semicolons:Luckily, the language has many other great features to counter this.