TypeError:Class.function不是Object的函数。<anonymous>

时间:2016-03-26 10:20:25

标签: node.js runtime-error javascript-objects 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);

});

我收到以下错误。

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

我无法找到问题所在。希望有人可以帮助我。 它基于战略模式,其中Bridges是我的界面。使用子类的类是Phue,Wemo和TcpConnect。出于某种原因,他无法看到我的物体。甚至是在server.js中添加的。

2 个答案:

答案 0 :(得分:0)

您正在导出Bridges的实例,而不是类。

module.exports = new Bridges(); // exporting instance of Bridges

您应该导出类/函数:

module.exports = Bridges;

更新:此外,您需要正确导出/导入。每个模块只能导出一个对象。在您的示例中,您可以导出一个包含所有函数的对象:

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

然后导入它:

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

答案 1 :(得分:0)

这是modules.exports的奇怪用法。在 interfaceBridge.js 中,您可以解除外部;function(){...}();并导出如下:

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

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

然后在 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;