2016-06-15 24 views
-1

私は、DynamoDBからuserLimitが低い値と高い値の間のデータを取得しようとしています。 JSONを使用してコードに低い値を送ります。私のNode.jsコード:Node.js Communcation with DynamoDB- JSON

var AWS = require('aws-sdk'); 
var db = new AWS.DynamoDB(); 


exports.handler = function(event, context) { 

    var low=event.ke1; 

    var params = { 
    TableName: "Events", //"StreamsLambdaTable", 
    ProjectionExpression: "userLimit, description", //specifies the attributes you want in the scan result. 
    FilterExpression: "userLimit between :lower and :higher", 
    // ExpressionAttributeNames: { 
    //  "#yr": "year", 
    // }, 
    ExpressionAttributeValues: { 
     ":lower": {"N": "low"}, 
     ":higher": {"N": "50"} 
    } 
}; 

    db.scan(params, function(err, data) { 
    if (err) { 
     console.log(err); // an error occurred 
     } 
    else { 
     data.Items.forEach(function(record) { 
      console.log(
       record.description.S + ""); 
     }); 
     context.succeed(data.Items); 
    // context.done(null,{"Result": "Operation succeeded."}); 
     //res.send(data.name); 
     } 
    // return next(); 
    }); 
}; 

私はエラーを取得する:

[ValidationException: ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: low for key :lower] 
    message: 'ExpressionAttributeValues contains invalid value: The parameter cannot be converted to a numeric value: low for key :lower', 

私は":lower": {"N": "low"},":lower": {"N": low},にエラーを変更した場合は次のとおりです。

{ [ValidationException: ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :lower] 
    message: 'ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :lower', 

私のJSONデータがある:

{ 
    "key1": "1" 
} 

方法その低い方の値をkey1の値で設定しますか?

EDIT:

ありました

間違いで:

var low=event.ke1; 

が可能Shuld

var low=event.key1;

EDIT 2:

私はそのコードを実行していたとき、別のエラーが発生しました:

var params = { 
    TableName: "Events", //"StreamsLambdaTable", 
    ProjectionExpression: "userLimit, description, type", //specifies the attributes you want in the scan result. 
    KeyConditionExpression: "type = :tempType", 
    // ExpressionAttributeNames: { 
    //  "#yr": "year", 
    // }, 
    ExpressionAttributeValues: { 
     ":tempType": {"S": "Party"} 
    } 
}; 

エラー:

{ [UnexpectedParameter: Unexpected key 'KeyConditionExpression' found in params] 
    message: 'Unexpected key \'KeyConditionExpression\' found in params', 

EDIT 3:

var AWS = require('aws-sdk'); 
var db = new AWS.DynamoDB(); 


exports.handler = function(event, context) { 

    var low=event.key1; 

    var params = { 
    TableName: "Events", //"StreamsLambdaTable", 
    ProjectionExpression: "ID, userLimit, description, #tp", //specifies the attributes you want in the scan result. 
    KeyConditionExpression: "#tp = :tempType", 
    ExpressionAttributeNames: { 
     "#tp": "type", 
    }, 
    ExpressionAttributeValues: { 
     ":tempType": {"S": "Party"} 
    } 
}; 

    db.query(params, function(err, data) { 
    if (err) { 
     console.log(err); // an error occurred 
     } 
    else { 
     data.Items.forEach(function(record) { 
      console.log(
       record.description.S + ""); 
     }); 
     context.succeed(data.Items); 
    // context.done(null,{"Result": "Operation succeeded."}); 
     //res.send(data.name); 
     } 
    // return next(); 
    }); 
}; 

しかし、私はエラーを取得::

私の現在のコードがある

{ [ValidationException: Query condition missed key schema element: ID] 
    message: 'Query condition missed key schema element: ID', 

答えて

2

「KeyConditionを式 "はscan操作には適用されません。

アトリビュートがidexedフィールドの場合は操作をクエリに変更するか、スキャン操作でなければならない場合はFilterExpressionを使用する必要があります。

+0

をポイントiが経由してその下の値を取得したいということですJSON。 より明確にするにはLower = event.ke1 –

+0

json経由で値を低くしますか? SQL DBのサブクエリのように、最も低い値を動的に代入することを期待していますか? – Shibashis

+0

私は最初の質問を編集しました。それを確認して –

-1

あなたのハッシュキーとあなたが使用することができ、インデックスに応じて:

DynamoDBのクエリ:

var params = { 
    TableName: 'table_name', 
    KeyConditions: { // indexed attributes to query 
        // must include the hash key value of the table or index 
        // with 'EQ' operator 
     attribute_name: { 
      ComparisonOperator: 'EQ', // (EQ | NE | IN | LE | LT | GE | GT | BETWEEN | 
             // NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH) 
      AttributeValueList: [ { S: 'STRING_VALUE' }, ], 
     }, 
     // more key conditions ... 
    } 
}; 
dynamodb.query(params, function(err, data) { 
    if (err) console.log(err); // an error occurred 
    else console.log(data); // successful response 
}); 

やDynamoDBのスキャン:

var params = { 
    TableName: 'table_name', 
    Limit: 0, // optional (limit the number of items to evaluate) 
    ScanFilter: { // optional (map of attribute name to Condition) 

     attribute_name: { 
      ComparisonOperator: 'EQ', // (EQ | NE | IN | LE | LT | GE | GT | BETWEEN | 
             // NOT_NULL | NULL | CONTAINS | NOT_CONTAINS | BEGINS_WITH) 
      AttributeValueList: [ { S: 'STRING_VALUE' }, ], 
     }, 
     // more conditions ... 
    } 
}; 
dynamodb.scan(params, function(err, data) { 
    if (err) console.log(err); // an error occurred 
    else console.log(data); // successful response 
});