我是Javascript和DynamoDB的新手。我试图在nodejs中使用javascript的aws-sdk执行getitem。Dynamo表Cars的主索引是字符串"name"
var AWS = require('aws-sdk');
AWS.config.region='eu-west-1';
var db = new AWS.DynamoDB();
var params = {
TableName : 'Cars',
Key : {
"name" : {
"S" : "Volkswagen Dasher"
},
}
}
db.getItem(params, function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
console.log(data); // successful response
}
return next();
});
在运行上述.js文件时,我得到以下错误。
ubuntu@ubuntu:~/node$ node getItem.js
{}
/home/ubuntu/node_modules/aws-sdk/lib/request.js:30
throw err;
^ ReferenceError: next is not defined
at Response.<anonymous> (/home/ubuntu/node/getItem.js:21:10)
at Request.<anonymous> (/home/ubuntu/node_modules/aws-sdk/lib/request.js:353:18)
at Request.callListeners (/home/ubuntu/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/home/ubuntu/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/home/ubuntu/node_modules/aws-sdk/lib/request.js:595:14)
at Request.transition (/home/ubuntu/node_modules/aws-sdk/lib/request.js:21:10)
at AcceptorStateMachine.runTo (/home/ubuntu/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /home/ubuntu/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/home/ubuntu/node_modules/aws-sdk/lib/request.js:37:9)
at Request.<anonymous> (/home/ubuntu/node_modules/aws-sdk/lib/request.js:597:12)
请帮帮我。干杯!
很高兴看到您正在尝试DynamoDB !我真的不确定我理解你的代码的上下文,但是如果你的目标是做一个简单的GetItem调用,你不需要'return next()'语句。鉴于javascript的事件驱动性质,这些回调是异步的,并没有真正"返回"任何东西。相反,您应该检查响应(数据)并执行相应的操作。
即
dynamoDB.getItem(params, function(err, data) {
if (data) {
doSomethingWithItem(data.Item);
}
});
另外,如果你刚刚开始,我建议你看一下document-js-sdk,它是原始SDK之上的包装器,允许你使用字面量,如"string"而不是{S: "string"}。