Node callback
NeDB
下方正常工作,
Datastore = require 'nedb'
db = new Datastore
db.insert a: 'Hi!', (err, docs) ->
console.log docs
然后尝试将NeDB Node callback
转换为Bacon EventStream
,
Bacon = require('baconjs').Bacon
Datastore = require 'nedb'
db = new Datastore
insert = Bacon.fromNodeCallback db.insert, a: 'Hi!'
insert.onValue (docs) ->
console.log docs
为什么它在下面失败?
TypeError: Cannot call method 'push' of undefined
答案 0 :(得分:1)
以这种方式传递db.insert
时,会丢失其评估上下文(“this”不再是数据库)。尝试使用其他形式的fromNodeCallback
:
insert = Bacon.fromNodeCallback(db, 'insert', {a: 'Hi!'})