gpt4 book ai didi

javascript - DynamoDB batchWriteItem 在 node.js 中崩溃

转载 作者:搜寻专家 更新时间:2023-10-31 22:52:20 24 4
gpt4 key购买 nike

我正在尝试在 aws-sdk 中使用 batchWriteItem,但遇到了导致 Node 崩溃的错误。奇怪的是我的项目确实被写入了数据库。错误发生在 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 中的错误吗?

最佳答案

替换:

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

与:

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

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

关于javascript - DynamoDB batchWriteItem 在 node.js 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31817328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com