【问题标题】:"retryDelay" when nodejs uses GetItem in dynamodbnodejs在dynamodb中使用GetItem时的“retryDelay”
【发布时间】:2018-02-21 01:22:39
【问题描述】:

我正在我的应用程序中登录,我在本地使用 Dynamodb 并且工作正常,但是当我尝试连接到 AWS 中的 dynamodb 时出现错误:

{ 
 "retryDelay": 21.203729590174113 }
}

我的连接代码是下一个:

var AWS = require("aws-sdk");
AWS.config.update({endpoint: "https://dynamodb.us-east-2.amazonaws.com", region: "us-east-2"});
var credentials = new AWS.SharedIniFileCredentials({profile: 'default'});
AWS.config.credentials = credentials;

var docClient = new AWS.DynamoDB.DocumentClient(credentials);

//Verifica la exitencia de un usuario
exports.init_session = function(req, res) {
  console.log('Iniciando consulta con ' + req.body.user);
  var params = {
      TableName: "usuarios",
      Key:{
          "user": req.body.user
      },
       "ProjectionExpression": "pass"
  };
  docClient.get(params, function(err, data) {
      if (err) {
          console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
          res.send("Unable to read item. Error JSON:" + JSON.stringify(err, null, 2));
      } else {
        if(JSON.stringify(data) != '{}'){  //si el usuario existe
            if( req.body.pass != data.Item.pass){ //Si la contraseña es incorrecta
                res.send('Contraseña inválida');
            }else{
            req.session.user = req.body.user;
            res.redirect('/');
          }
        }else {
            res.send('No existe el usuario');
        }
      }
  });
};

当我在单个脚本中执行相同的请求时,它工作正常。

var AWS = require("aws-sdk");

AWS.config.update({endpoint: "https://dynamodb.us-east-2.amazonaws.com", region: "us-east-2"});
var credentials = new AWS.SharedIniFileCredentials({profile: 'default'});
AWS.config.credentials = credentials;

var docClient = new AWS.DynamoDB.DocumentClient()

var params = {
    TableName: "usuarios",
    Key:{
        "user": "user2"
    }
};

docClient.get(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});

我通过的问题是req.body.user,但是在console.log 中显示出来和字符串。什么问题,为什么会出现 {"retryDelay": random_number }?

【问题讨论】:

    标签: node.js amazon-web-services express amazon-dynamodb aws-sdk


    【解决方案1】:

    The AWS config is part of the core AWS SDK

    当您在本地运行时,您的默认配置可能会从profile file 处加载

    ~/.aws/config
    

    我不太确定在其他环境中从哪里加载您的配置,但我可以看到您正在更新您的配置 (AWS.config.update) 而不是创建一个新配置,这看起来像:

    var config = new AWS.Config({
      accessKeyId: 'AKID', secretAccessKey: 'SECRET', region: 'us-west-2'
    });
    

    【讨论】:

      【解决方案2】:

      有点尴尬,我的错误是这一行。

      var docClient = new AWS.DynamoDB.DocumentClient(credentials);
      

      我删除了 var 凭据,它工作正常。

      var docClient = new AWS.DynamoDB.DocumentClient();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-13
        • 1970-01-01
        • 2023-03-29
        • 2013-10-05
        • 1970-01-01
        • 2020-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多