DynamoDB batchWriteItem在node.js中崩溃

时间:2015-08-04 19:09:53

标签: javascript node.js amazon-web-services

我试图在aws-sdk中使用batchWriteItem并遇到崩溃节点的错误。奇怪的是我的物品被写入数据库。错误发生在sdk内。

我的代码是:

function sendItems(params) {
    console.log("SENDING:");
    console.log(params);
    dynamodb.batchWriteItem(params, function(err, data) {
        console.log("Response from DynamoDB");
        if(err) console.log(err);
        else    console.lod(data);
    }); 
}

var toSave = [];
for(name in names) {
    var item = {
      Id:         { S: uuid.v4() },
      Name:       { S: name }
    };
    toSave.push(item);
}

var items = [];
for(var i = 0; i < toSave.length; i++) {
    items[i] = {
        PutRequest: { Item: toSave[i] }
    }
}

var params = {
    RequestItems: {
        'MyTableName': items
    },
    ReturnConsumedCapacity: 'TOTAL',
    ReturnItemCollectionMetrics: 'SIZE'
};

sendItems(params);

错误的堆栈跟踪是:

/Users/gsimons/node_modules/aws-sdk/lib/request.js:30
        throw err;
              ^
TypeError: undefined is not a function
at Response.<anonymous> (/Users/gsimons/Documents/workspace/testing/aws-put-items.js:62:21)
    at Request.<anonymous> (/Users/gsimons/node_modules/aws-sdk/lib/request.js:353:18)
    at Request.callListeners (/Users/gsimons/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/Users/gsimons/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/Users/gsimons/node_modules/aws-sdk/lib/request.js:595:14)
    at Request.transition (/Users/gsimons/node_modules/aws-sdk/lib/request.js:21:10)
    at AcceptorStateMachine.runTo (/Users/gsimons/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/gsimons/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/gsimons/node_modules/aws-sdk/lib/request.js:37:9)
    at Request.<anonymous> (/Users/gsimons/node_modules/aws-sdk/lib/request.js:597:12)

现在我真正得到的是当我看到node_modules / aws-sdk / lib / request.js时:

21:    self.emit(self._asm.currentState, function(err) {
22:      if (err) {
23:        if (isTerminalState(self)) {
24:          if (domain && self.domain instanceof domain.Domain) {
25:            err.domainEmitter = self;
26:            err.domain = self.domain;
27:            err.domainThrown = false;
28:            self.domain.emit('error', err);
29:          } else {
30:            throw err;
31:          }
32:        } else {
33:          self.response.error = err;
34:          done(err);
35:        }
36:      } else {
37:        done(self.response.error);
38:      }
39:    });

实际上有以下情况似乎很奇怪:

if(err) {
  //err must be defined here, right?
  //No code modifies err or sets it to undefined.
  //Is it possible that another thread altered err
  //  between the call to the function and the call to throw?
  throw err; //Should just throw err back up.
}

这可能是aws js sdk中的错误吗?

1 个答案:

答案 0 :(得分:1)

替换:

for(name in names) {
    var item = {
      Id:         { S: uuid.v4() },
      Name:       { S: name }
    };
    toSave.push(item);
}

with:

var toSave = [];
names.forEach(function(name){
    var item = {
      Id:         { S: uuid.v4() },
      Name:       { S: name }
    };
    toSave.push(item);
});

我遇到了同样的问题,但上面的解决方案为我解决了这个问题。假设您的代码中的名称变量是一个数组。