1
var Subscription = require('./Subscription');
var api = require('./api');

exports = module.exports = {};
var bridgeData;

function validateAccount(account) {
  if (!account) throw new Error('Muzzley account details must be present!');
  if (!account.appToken) throw new Error('appToken is not defined!');
  if (!account.profileId) throw new Error('profileId is not defined!');
  if (!account.serialNumber) throw new Error('serialNumber is not defined!');
}

function validateComponents(components) {
  if (!components || components.length === 0) throw new Error('Bridge components must be defined!');

  for (var i = 0; i != components.length; ++i) {
    if(!components[i].id) throw new Error('Missing component id!');
    if (!components[i].type) throw new Error('Missing component type!');
    if (!components[i].label) throw new Error('Missing component label!');
  }
}

exports.connect = function (account, components, callback) {

  validateAccount(account);
  validateComponents(components);

  bridgeData.appToken = account.appToken;
  bridgeData.profileId = account.profileId;

  api.registerBridge(account, function (err, bridge) {
    if (err) return callback(err);

    bridgeData.deviceKey = bridge.deviceKey;

    api.updateBridge(bridge, components, function (err) {
      if (err) return callback(err);

      var subscription = new Subscription(account.profileId, account.appToken);
      subscription.load(function (err) {
        return callback(err, subscription);
      });
    });
  });
};

exports.updateComponents = function (components, callback) {
  api.updateBridge(bridgeData, components, function (err) {
    return callback(err);
  });
};

有一个名为 undefined 的错误:

ERROR: /home/root/.node_app_slot/node_modules/muzzley-bridge-node/lib/index.js:29                                                                                     
ERROR:   bridgeData.appToken = account.appToken;                                                                                                                      
ERROR:                       ^                                                                                                                                        
ERROR: TypeError: Cannot set property 'appToken' of undefined
4

3 回答 3

1

您在代码中声明 bridgeData 错误。

只需更改以下行:

var bridgeData;

至:

var bridgeData = {};

当您这样做时:var bridgeData;您的变量未定义,并且您不能将新属性分配给未定义的变量。

于 2015-11-23T04:48:12.370 回答
0

我刚刚写信给作者。
他修复了库 -> ver.0.0.4 。
现在'npm update muzzley-bridge-node'!

于 2017-01-11T13:35:48.463 回答
0

您是否使用此库将板连接到 Muzzley ?它已经被修复了。跑:

npm install muzzley-bridge-node

安装最新版本

于 2015-11-24T11:17:04.970 回答