I have some RxJS code using mongo to perform queries. Works fine in RxJS v4 but as I am migrating to v.5 I am running into issues.
Here's some simplified code:
// Get a mongo connection
getDb() {
var connect = Rx.Observable.bindNodeCallback(mongodb.connect)
return connect(this.options.connection.uri, this.options.connection.options)
}
// Query
return getDb()
.flatMap((db) => {
var c = db.collection('foo')
var obs = Rx.Observable.bindNodeCallback(c.insertMany, c)
return obs(docs)
})
.subscribe(...)
Every time I try some sort of query it fails with various errors. All the errors are related to an options
object not existing inside the Mongo code. I think this may be a context issue but I am not sure.
The query above yields (in the code, undefined is a mongo collection options object)
Uncaught TypeError: Cannot read property 'serializeFunctions' of undefined
at BoundNodeCallbackObservable.Collection.insertMany[as callbackFunc](node_modules / mongodb / lib / collection.js: 482: 74)
A similar query yields:
TypeError: Cannot read property 'options' of undefined
at BoundNodeCallbackObservable.Collection.remove[as callbackFunc](node_modules / mongodb / lib / collection.js: 1223: 12)
Update When I wrap it manually things work:
var obs = Rx.Observable.create(function(observer) {
c.insertMany(docs, function(err, res) {
if (err) { observer.error(err) } else {
observer.next(res);
observer.complet();
}
})
})
return obs
// This doesn't:
var obs = Rx.Observable.bindNodeCallback(c.insertMany, c)
return obs(docs)