나는 (미리 복잡성에 대한 사과)과 같이 보이는 일부 커피 스크립트가 : 그것은 해야하는자바 스크립트/커피 스크립트 인수로 함수의 해시를 사용
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
문서의 일종을 만들어에 삽입 또는 업데이트를 구현하기를 콜백으로서의 데이타베이스.
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'))
당신이 볼 수 있듯이
는needs_update
기능은
console.log
문 박혀있다. 이것은 node.js에서 실행되고 있으며 실행시 한 번 실행됩니다. 따라서 노드 속성에서보기는 쉽지 않습니다. 적어도 나는 방법을 알아 내지 못했다.
어쨌든이 흥미로운 부분은 console.log inspect arguments
입니다. inspect
은 객체를 JSON 문자열로 변환하기 때문에 읽을 수 있습니다. 결과는 항상 {"0":{}}
입니다.
그리고 그것이 내가 붙어있는 곳입니다. 해시가 전달되는 것을 볼 수 있지만 해시에는 아무 것도 없습니다. 낯선 사람이 여전히 순수 Javascript로 손을 쓸 때 이와 동일한 문제가 발생합니다.
나는 이것을 줄이고 아무 쓸모없이 재현하려고 노력했다. 이 코드는 작동합니다 :
h =
f1: (a) -> 'one'
f2: (b) -> 'two'
test = (fn) ->
console.log fn.f1.call()
console.log fn.f2.call()
test(h)
왜 첫 번째 코드가 실패하고 축소 된 예제가 작동하는지 누가 알 수 있습니까?
감사합니다.
예. JSON.stringify() (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)를 호출합니다. –
@ muistooshort-- 'callbacks not null'로 시작하는'console.log'는 여러분이 말하는 것과 같습니다 : 그것은 내가 기대하는 두 개의 키/값 쌍을 검사합니다. 둘 다'undefined '가됩니다. 나는 그것을 단순화하려고 노력했으나, 핵심에서 왜 논쟁이 빈 해시로 나타나는지 이해할 수 없다. –
코드가 괜찮습니다. 잘못된 점은 콜백이 제공되고 있음에도 불구하고 'throw'가 '_.isEmpty (callbacks.update)이면 업데이트 콜백이 필요합니다'이고 insert에 대해 동일한 콜백이 필요하다는 것입니다. 어떤 이유로 든, 제공된 함수는 Underscore에 의해 비어있는 것으로 평가됩니다. 예외를 던진 행을 제거하면 문제가 해결되었습니다.그래서 질문을 다시 짜낼 것입니다. 콜백 부재에 대한 이러한 테스트가 사실로 평가되는 이유는 무엇입니까? –