Dynamo DB getItem与节点js抛出错误

时间:2015-07-14 08:02:57

标签: javascript node.js amazon-dynamodb aws-sdk

我是Javascript和DynamoDB的新手。我试图在nodejs中使用aws-sdk为javascript执行getitem。迪纳摩表汽车的主要指标是"名称"一个字符串。

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)

Plz帮助我。干杯!

1 个答案:

答案 0 :(得分:2)

很高兴看到您尝试使用DynamoDB!我不确定我是否了解您的代码的上下文,但如果您的目标是进行简单的GetItem调用,那么您就不需要返回下一个()&#39;声明。鉴于javascript的事件驱动性质,这些回调是异步的,并且不会真正地返回&#34;任何东西。相反,您应该检查response(数据)并相应地执行操作。

dynamoDB.getItem(params, function(err, data) {
                     if (data) {
                        doSomethingWithItem(data.Item);
                     }
                 });

此外,如果您刚刚开始我会建议您查看原始SDK顶部的document-js-sdk包装器,以便您使用&#34;字符串&#等字面值34;而不是{S:&#34; string&#34;}。