I used coffee-script to write node.js, it works fine, what i want to know is how to return a value for a function in coffee-script, here is my code
exports.find=->
db.open((err, db)->
unless err
db.createCollection('test', (err, collection)->
unless err
collection.find().toArray((err, items)->
unless err
//here how to return *items*
console.log(items)
)
)
)
Since the DB related operations are all asynchronous, you should specify a callback function for the
findroutine.(Sidenote:
you should use guard clauses like
return if errinstead ofunless err. It decreases the indentation level and makes the code easier to read.Or better yet, you should pass the error to the callback as the first parameter. This is the convention for node.js projects)