Strategie Muster: interfaceBridge.jsTypeError: Class.function ist keine Funktion bei Object. <anonymous>
// 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);
});
Ich erhalte den folgenden Fehler.
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
Ich kann nicht finden, was das Problem ist. Hoffe jemand könnte mir helfen. Es basiert auf Strategie-Muster, wo Bridges meine Schnittstelle ist. Klassen, die die Unterklasse verwenden, sind Phue, Wemo und TcpConnect. Aus irgendeinem Grund kann er meine Objekte nicht sehen. Sogar die sind in der server.js hinzugefügt.
'var Brücken in interfaceBridge.js können Sie die äußeren
;function(){...}();
und Exporte wie entlassen = Bridges' sieht nicht aus wie Initialisieren von Objekten. Vielleicht sollte es "new Bridges()" sein. –