2

I'm using Bacon.js on the server side with Express.JS (version 4.x)

This method never responds! Am I doing something extremely wrong here?

var User = require('../data/User');
var Bacon = require('baconjs').Bacon;

module.exports = function(router){
       router.route('/users').get(function(req, res){

           var result = Bacon.fromNodeCallback(User.find, null);

           result.onValue(function(docs){
               res.status(200).send(docs);
           });

           result.onError(function(err){
               res.status(500).send(err);
           });

    });
};

Apparently I'm failing with RxJS as well...

var User = require('../data/User');
var Rx = require('rx');

module.exports = function(router){
    router.route('/users').get(function(req, res){

        var query = Rx.Observable.fromNodeCallback(User.find)();

        query.subscribe(function(docs){
            res.send(docs);
        }, function(err){
            res.send('Error: ' + err);
        }, function(){
            console.log("COMPLETED");
        })

    });
};

This is the error that is returned:

Error: TypeError: Cannot read property 'discriminatorMapping' of undefined
Max Alexander
  • 5,471
  • 6
  • 38
  • 52
  • This looks ok. Is the `null` parameter intentional? Do you mean to call User.find with null as in: `User.find(null, function(err, docs) { ...` – OlliM Dec 08 '14 at 12:57
  • Yes I put that null in there to test giving it an optional parameter. – Max Alexander Dec 08 '14 at 13:20
  • And `User.find` works as expected? Basically, if you skip bacon completely and just write `User.find(null, function(err, docs) { if(err) { res.status(500).send(err); } else { res.status(200).send(docs); }})`, does that work? – OlliM Dec 08 '14 at 13:25
  • Yes it works perfectly fine with the normal API – Max Alexander Dec 08 '14 at 13:26
  • 3
    Could this be a problem with binding `this`? You could try giving `User.find.bind(User)` instead of `User.find` as the parameter. – OlliM Dec 08 '14 at 13:43
  • Wow that worked perfect. This also worked: Rx.Observable.fromPromise(User.find({}).exec());. However, both of these syntax look extremely unfriendly and overkill. – Max Alexander Dec 08 '14 at 13:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66406/discussion-between-max-alexander-and-ollim). – Max Alexander Dec 08 '14 at 13:50

1 Answers1

2

You should use Bacon.fromNodeCallBack with an object:

Bacon.fromNodeCallback(User, "find", null);

That will bind this correctly for your code.

OlliM
  • 7,023
  • 1
  • 36
  • 47