diff --git a/lib/Table.js b/lib/Table.js index 2902cdc50..0041001a6 100755 --- a/lib/Table.js +++ b/lib/Table.js @@ -27,8 +27,8 @@ var compareIndexes = function compareIndexes(local, remote) { var i; var j; - var localIndexes = localTableReq.GlobalSecondaryIndexes; - var remoteIndexes = remoteTableReq.GlobalSecondaryIndexes; + var localIndexes = localTableReq.GlobalSecondaryIndexes || []; + var remoteIndexes = remoteTableReq.GlobalSecondaryIndexes || []; debug('compareIndexes'); // let's see what remote indexes we need to sync or create @@ -168,11 +168,13 @@ Table.prototype.init = function (next) { deferred.reject('indexes are not synchronized and update flag is set to false'); } } - table.waitForActive(); - //table.active = data.Table.TableStatus === 'ACTIVE'; table.initialized = true; + return table.waitForActive() + .then(function () { + //table.active = data.Table.TableStatus === 'ACTIVE'; - return deferred.resolve(); + return deferred.resolve(); + }); }) .catch(function (err) { if (err && err.code === 'ResourceNotFoundException') { @@ -182,15 +184,15 @@ Table.prototype.init = function (next) { .then(function () { table.initialized = true; }) - // .then(function() { - // if(table.options.waitForActive) { - // return table.waitForActive(); - // } - // }) + .then(function() { + if(table.options.waitForActive) { + return table.waitForActive(); + } + }) ); } if (err) { - debug('error initializing', err); + debug('error initializing', err.stack); return deferred.reject(err); } }); @@ -219,6 +221,7 @@ Table.prototype.waitForActive = function (timeout, next) { var timeoutAt = Date.now() + timeout; function waitForActive() { + debug('Waiting...'); /* if (table.active) { debug('Table flag is set to Active - %s', table.name); @@ -235,7 +238,8 @@ Table.prototype.waitForActive = function (timeout, next) { table.describe() .then(function (data) { var active = (data.Table.TableStatus === 'ACTIVE'); - data.Table.GlobalSecondaryIndexes.forEach(function (gsi) { + var indexes = data.Table.GlobalSecondaryIndexes || []; + indexes.forEach(function (gsi) { //debug('waitForActive Index Check: %s', JSON.stringify(gsi, null, 2)); debug('index %s.IndexStatus is %s', gsi.IndexName, gsi.IndexStatus); if (gsi.IndexStatus !== 'ACTIVE') { @@ -251,6 +255,10 @@ Table.prototype.waitForActive = function (timeout, next) { } }) .catch(function (err) { + if (err && err.code === 'ResourceNotFoundException') { + return setTimeout(waitForActive, 10); + } + debug('Error waiting for active', err.stack); return deferred.reject(err); }); }