I have some Coffeescript that looks like this (apologies for the complexity in advance):
doc = new ChargerServerDoc(Chargers.find({id:site.id}), site)
doc.set_defaults().merge().needs_update
update: (id, doc) ->
Chargers.update id, $set: doc, (error, result) ->
if error
run_stats.error_count += 1
"error"
else
run_stats.update_count += 1
"update"
return
insert: (doc) ->
Chargers.insert doc, (error, result) ->
if error
run_stats.error_count += 1
"error"
else
run_stats.insert_count += 1
"insert"
return
It's supposed to create some sort of document and implement insert or update to the database as callbacks.
needs_update: (callbacks = null) ->
console.log inspect arguments
if callbacks is null
return true unless @is_equal(@working_document, @retrieved_document)
return false
else
console.log """
callbacks not null:
insert: #{inspect callbacks['insert']}
update: #{inspect callbacks['update']}
"""
data = @get()
if @is_equal(@working_document, @retrieved_document)
throw 'requires update callback' if _.isEmpty(callbacks.update)
return callbacks.update.call(this, data._id, _.omit(data, '_id'))
else
throw 'require insert callback' if _.isEmpty(callbacks.insert)
return callbacks.insert.call(this, _.omit(data, '_id'))
As you can see, the needs_update function is peppered with console.log statements. This is running in node.js, and it's a run-once-at-startup thing. So it's not easy to watch in the node inspector. At least I haven't figured out how.
In any case, the interesting part of this is the console.log inspect arguments. inspect just converts the object to a JSON string so I can read it. And the result is always {"0":{}}.
And that's where I'm stuck. I can see it's passing a hash, but there's nothing in the hash. Stranger still, this same behavior occurs when I hand write it in pure Javascript.
I tried to reduce this and reproduce it to no avail. This code works:
h =
f1: (a) -> 'one'
f2: (b) -> 'two'
test = (fn) ->
console.log fn.f1.call()
console.log fn.f2.call()
test(h)
Does anyone see why the first code fails and the reduced example works?
Thanks!
console.logthat starts withcallbacks not nulldoes very much what you say: It examines the two key/value pairs I expect and they both come upundefined. I've tried to simplify it, but at the core, I don't understand why the arguments are turning up with an empty hash.throw 'requires update callback' if _.isEmpty(callbacks.update)and the same for insert were firing even though the callbacks were being provided. For some reason, the provided functions evaluated as empty by Underscore. Removing the lines that threw the exceptions solved the problem. So I'll reframe the question: Why would these tests for absence of a callback evaluate to true?