1
 var query = Rx.Observable.fromCallback(dbConnection.query);
 var obs = query('select * from mytable where id = ?', ['389']);
 obs.subscribe(
     function(e) = {}
 );

This code is giving me TypeError: Cannot read property 'typeCast' of undefined.

The documentation for MySqlJs query method looks like this:

connection.query('SELECT * FROM `books` WHERE `author` = ?', ['David'], function (error, results, fields)

When I log query it returns [Function]. What am I doing wrong?

Stampede10343
  • 844
  • 1
  • 6
  • 21

1 Answers1

1

The query function will be passed to fromCallback, but when it's called it will not be bound to dbConnection.

You should do something like this:

Rx.Observable.fromNodeCallback(dbConnection.query.bind(dbConnection));

Also, you probably want fromNodeCallback.

cartant
  • 57,105
  • 17
  • 163
  • 197
  • This appears to work, I am using Node though. What does bind() do and why fromNodeCallback() it looks the same to me. Sorry I'm a Java developer so I'm struggling with the language a bit. – Stampede10343 Apr 05 '17 at 01:26
  • 1
    Node callbacks follow the pattern of having an error as the first parameter and `fromNodeCallback` takes that into account. And `bind` is needed because `this` is weird in JS: http://stackoverflow.com/a/41391426/6680611 – cartant Apr 05 '17 at 02:07
  • thanks for that! That makes a lot of sense. Coming from Java this is super clear, but the dynamicness of JS makes for weird stuff like this. Thank you! – Stampede10343 Apr 05 '17 at 02:44