Hello i try to read somethink from my sqlite in coffe.script when i wrote it JS it works well but now i got some problem
Coffee.script: I am new in coffeescript and i am wondering what am i doing wrong... Any tips guys ? :)
app.get('/indeks',
(req, res)->
tab = []
i = 0
db = new sqlite3.Database("xxx.sqlite3")
tab = []
i=0
console.log("Jestem przed dbHandler")
db.each("SELECT yyy FROM zzz", @dbHandler, @dbFinal
dbHandler:(err, row)->
console.log("I am in handler dbHandler")
if err
console.log("Error: " + err)
else
tab.push(row)
console.log(row)
dbFinal:()->
console.log("I am in dbFinal")
console.log("Final: " + tab)
console.log("Response")
res.send(tab)
db.close()
)
)
Now code in JS:
app.get('/indeks', function (req, res, next) {
var db = new sqlite3.Database("xxx");
var tab = new Array();
var i=0;
function dbHandler(err, row){
if (err) {
console.log("Error: " + err);
} else {
tab.push(row);
console.log(row);
}
}
function dbFinal(){
console.log("Final: " + tab);
console.log("Response");
res.send(tab);
}
db.each("SELECT zzz FROM yyy", dbHandler, dbFinal);
db.close();
});
Did you look into the transpiled coffee code? When using something like dbHandler:(err, row)-> a JSON-Object with the property dbHandler is generated. This is why you cannot pass dbHandler and dbFinal to the db.each call. This only works when defining a class.
Additionally, you got an unmatched bracket in the line 10 and a bracket too much in the last two lines.
You should always check the compiled code (respectively check whether it even compiles). Here is a helpful site for this. There, you can even convert your JS code to coffeescript.