Search code examples
node.jsruntime-errorjavascript-objectsstrategy-pattern

TypeError: Class.function is not a function at Object.<anonymous>


Strategy Pattern: interfaceBridge.js

    // START IMPORT NODE HUE API
    var hue = require("node-hue-api"),
    HueApi = hue.HueApi,
    lightState = hue.lightState;
    var displayResult = function(result) {
        console.log(result);
    };

    var displayError = function(err) {
        console.error(err);
    };


    var host = "192.168.2.129",
    username = "033a6feb77750dc770ec4a4487a9e8db",
    api = new HueApi(host, username),
    state = lightState.create();

// END IMPORT NODE HUE API

var Bridges = function() {
  this.bridge = "";
};

Bridges.prototype = {
    setBridge: function(bridge){
      this.bridge = bridge;
    },

    showAllLights: function() {
    },

    getStatusOfLamp: function(id){
    },

    setLightState: function(id, state){
    }

};

var Phue = function() {
    this.showAllLights = function() {
      api.lights(function(err, lights) {
        if (err) throw err;
        displayResult(lights);
      });
    },
    this.getStatusOfLamp = function(id){
      api.lightStatus(id, function(err, result) {
        if (err) throw err;
        displayStatus(result);
      });
    },
    this.setLightState = function(id, state){
      if(state == 'on'){
        api.setLightState(id, state.on(), function(err, result) {
             if (err) throw err;
             displayResult(result);
        });
      } else {
        api.setLightState(id, state.off(), function(err, result) {
          if (err) throw err;
          displayResult(result);
        });
      }

    }
};

var Wemo = function() {
    this.showAllLights = function() {
        //@String from Belkin WeMo API
    },
    this.getStatusOfLamp = function(id){
      //@String from Belkin WeMo API
    },
    this.setLightState = function(id, state){

    }
};

var TcpConnect = function() {
    this.showAllLights = function() {
        //@String from TCP Connect API
    },
    this.getStatusOfLamp = function(id){
      //@String from TCP Connect API
    },
    this.setLightState = function(id, state){

    }
};

module.exports = {
    Bridges: Bridges,
    Phue: Phue,
    Wemo: Wemo,
    TcpConnect: TcpConnect
};

server.js

var express = require('express');
var app = express();
var fs = require("fs");
var Bridges = require("./interfaceBridge.js").Bridges;
var Phue = require("./interfaceBridge.js").Phue;
var Wemo = require("./interfaceBridge.js").Wemo;
var Tcpconnect = require("./interfaceBridge.js").TcpConnect;

// Initialising of objects

var bridges = Bridges;
var phue = Phue;
var wemo = Wemo;
var tcpconnect = Tcpconnect;
bridges.setBridge(phue);



var PORT = 4333;
app.get('/lights/all', function (req, res) {
  bridges.showAllLights();
});

app.get('/lights/:id/state', function (req, res) {
  var id= req.params.id;

  bridges.getStatusOfLamp(id);
});

app.get('/lights/:id/state/:state', function (req, res) {
  var id = req.params.id;
  var state;

  bridges.setLightState(id, state);
});
var server = app.listen(PORT, function () {

  var host = server.address().address;
  var port = server.address().port;

  console.log("Example app listening at http://%s:%s", host, port);

});

I recieve the following error.

TypeError: bridges.setBridge is not a function
    at Object.<anonymous> (/Users/jensgryspeert/Documents/STAGE/Raspberry Rest API/server.js:12:9)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:136:18)
    at node.js:972:3

I can't find what the problem is. Hope someone could help me out. It's based on Strategy pattern where Bridges is mine interface. Classes that use the subclass are Phue, Wemo and TcpConnect. For some reason he can't see mine objects. Even the are added in the server.js.


Solution

  • It's a strange usage of modules.exports. In interfaceBridge.js, You can dismiss the outer ;function(){...}(); and exports like:

    modules.exports = {
        Bridges: Bridges,
        Phue: Phue,
        Wemo: Wemo,
        TcpConnect: TcpConnect
    };
    

    or

    exports.Bridges = Bridges;
    exports.Phue = Phue;
    exports.Wemo = Wemo;
    exports.TcpConnect = TcpConnect;
    

    Then require in server.js:

    var Bridges = require("./interfaceBridge.js").Bridges;
    var Phue = require("./interfaceBridge.js").Phue;
    var Wemo = require("./interfaceBridge.js").Wemo;
    var TcpConnect = require("./interfaceBridge.js").TcpConnect;