Trying to pass something to something else. Says undefined. Not sure how or why.
clsapp.on 'mysqld', ->
getHostById = (host) -> cls.getHostById.acq host, (c) -> JSON.stringify(result)
I need some way to access getHostById('localhost').hostname while inside another part of the .js
clsapp.on getHostById('localhost'), (c) ->
console.log JSON.parse(getHostById('localhost')).hostname
is just null or undefined
It’s hard to understand what you’re asking, but I’ll make some observations:
In the code
you probably meant for the callback argument to be called
result, notc. Unless you’re definingresultelsewhere.More importantly, there has got to be a clearer way for you to write this code. Just expanding the definition of
getHostByIdto be multi-line helps a bit:Now, I think that ultimately, your problem is that you’re trying to make an async function behave synchronously—which you can’t do in JavaScript (or CoffeeScript, which is a thin syntactic layer on top of JS). Since
cls.getHostById.acqtakes a callback, it’s almost certainly designed to call that callback after it returns so that it doesn’t block the thread. Which means that there’s no way for you to write agetHostByIdfunction that simply returns a value, as yourJSON.parse(getHostById('localhost'))example suggests. You’ll have to use a callback.