简体   繁体   English

带有节点JS抛出错误的Dynamo DB getItem

[英]Dynamo DB getItem with node js throwing error

I am new to Javascript and DynamoDB. 我是Java和DynamoDB的新手。 I am trying to perform getitem using aws-sdk for javascript in nodejs. 我正在尝试使用aws-sdk为nodejs中的javascript执行getitem。 primary index of Dynamo table Cars is "name" a string. Dynamo表Cars的主要索引是“名称”一个字符串。

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();
                });

On running the above .js file I am getting the following error. 运行上面的.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 help me out. 请帮我。 Cheers! 干杯!

Glad to see you're giving DynamoDB a try! 很高兴看到您正在尝试DynamoDB! I'm not really sure I understand the context of your code, but if your goal is to make a simple GetItem call, you don't need the 'return next()' statement. 我不确定我是否了解您的代码的上下文,但是如果您的目标是进行简单的GetItem调用,则不需要'return next()'语句。 Given javascript's event driven nature, these callbacks are asynchronous and don't really "return" anything. 给定javascript的事件驱动性质,这些回调是异步的,实际上并没有“返回”任何东西。 Instead, you should inspect the response (data) and perform an action accordingly. 相反,您应该检查响应 (数据)并相应地执行操作。

IE IE浏览器

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

Also, if you're just starting out I would recommend taking a look at the document-js-sdk which a wrapper on top of the original SDK to allow you to use literals such as "string" instead of {S: "string"}. 另外,如果您只是刚入门,我建议您看一下document-js-sdk ,它是原始SDK的包装,可让您使用“ string”之类的文字而不是{S:“ string” }。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM