Jackie Jackie - 1 year ago 176
Javascript Question

"TypeError: this._subscribe is not a function" When trying RxJS in simple Node Module

I have the following node code

var username = process.argv[2],
password = process.argv[3],
factories = require('./factories')(username, password);


var Rx = require('rx');
var factories = function(username, password){
var factories = [];
var source = Rx.Observable.from(factories);
var baseUrl = "<url>",
factoriesOpt = {
'uri': baseUrl+"<path>",
'method': 'GET',
'auth': {
'user': username,
'pass': password
request = require('request');

var refresh = function(){
.on('data', function(chunk){
response += chunk;
.on('end', function(){
factories = JSON.parse(response);
return {
subscribe : source.subscribe,
factories : factories,
refresh : refresh
module.exports = factories;

But when I run this I get the error...

TypeError: this._subscribe is not a function
at Object.Rx.Observable.observableProto.subscribe.observableProto.forEach [as subscribe] (/Users/<code location>/node_modules/rx/dist/rx.js:2034:19)
at Object.<anonymous> (/Users/<code location>/index.js:5:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3


If I change factory to use...

var subscribe = function(callback){
return {
subscribe : subscribe,
factories : factories,
refresh : refresh

It works but I would like to avoid wrapping.

Answer Source

This is a binding issue and one of the fine points of javascript. Won't go into details now, but basically instead of source.subscribe you should do source.subscribe.bind(source). If that works, let me know, I'll put more detail on the binding issue.

Just found a reference describing the binding mistake : http://developer.telerik.com/featured/seven-javascript-quirks-i-wish-id-known-about/#functions. Have a look at #3.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download