节点错误 - 无法读取未定义的属性

时间:2016-01-06 20:30:41

标签: node.js

我正在尝试编写一个与节点包一起使用的节点模块。我有点卡住,不知道问题出在哪里。我知道正确加载包。

以下代码引用了icontrol包 https://github.com/nfarina/icontrol/blob/master/index.js

// loads the icontrol package
var iControl = require("node-icontrol").iControl;
// these are the paramters you need
// according to this https://github.com/nfarina/icontrol/blob/master/index.js#L19-L22
var config = {
  system: "system name",
  email: "your@email.com",
  password: "kdkdkdkdk",
  pinCode: "pin"
}
// then you will want to initialize it
var mySystem = iControl(config);
// then there are a few calls you can make on mySystem
// 1. getArmState https://github.com/nfarina/icontrol/blob/master/index.js#L56
mySystem.getArmState(function(error, result){
if (error) {
console.error(error);
return
  }
console.log(result);
});
// 2. setArmState https://github.com/nfarina/icontrol/blob/master/index.js#L70
// it looks like the armState param can be "disarm" or "arm"
mySystem.setArmState(armState, function(error){
if (error) {
console.error(error);
return
  }
console.log("Alarm set to:", armState);
});
// 3. subscribeEvents https://github.com/nfarina/icontrol/blob/master/index.js#L96
// this will open a web socket listener that will send a message any
// time the arm state is changed

我收到错误

mySystem.getArmState(function(error, result){
        ^
TypeError: Cannot read property 'getArmState' of undefined
at Object.<anonymous> (/Users/Admin/Documents/Test.js:15: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:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您忘记了new运营商。 iControl函数用作构造函数(使用new)。它没有明确的return语句,因此如果没有new,则会返回undefined

变化:

var mySystem = iControl(config);

要:

var mySystem = new iControl(config);