Search code examples
javascriptnode.jscoffeescriptbacon.jsnedb

Failed to convert NeDB Node callback to Bacon EventStream


Below the Node callback on NeDB worked correctly,

Datastore = require 'nedb'
db = new Datastore

db.insert a: 'Hi!', (err, docs) -> 
    console.log docs

Then tried to convert NeDB Node callback to 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

Why did it fail below?

TypeError: Cannot call method 'push' of undefined

Solution

  • When you pass db.insert that way, you lose its evaluation context ("this" is no longer the database). Try using the other form of fromNodeCallback:

    insert = Bacon.fromNodeCallback(db, 'insert', {a: 'Hi!'})