2016-10-05 43 views
1

當我做從dynamoDB使用的NodeJS客戶端的結果包含的類型作爲重點即如何重新從的NodeJS客戶

{ 
    Item: { 
     newrelic_id: { 
      S: 'nr-1234' 
     } 
    , 
     jumpcloud_id: { 
      S: 'j-234' 
     } 
    , 
     microtime: { 
      N: '1475690490854' 
     } 
    , 
     instance_id: { 
      S: 'i-abc1234' 
     } 
    } 
} 

注意的響應AWS GET請求DynamoDB響應的鑰匙值是類型的前綴,SStringNNumber有沒有辦法刪除這個「類型鍵」?

+1

我建議使用DocumentClient,因爲它會自動爲您轉換所有這些。 '新的AWS.DynamoDB.DocumentClient()' – idbehold

回答

2

以下是使用DocumentClient的示例代碼。

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

var creds = new AWS.Credentials('akid', 'secret', 'session'); 

AWS.config.update({ 
    region : "us-west-2", 
    endpoint : "http://localhost:8000", 
    credentials : creds 
}); 

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

var table = "Movies"; 

var year_val = 2015; 
var title = "The Big New Movie"; 

var params = { 
    TableName : table, 
    KeyConditionExpression : 'yearkey = :hkey and title = :rkey', 
    ExpressionAttributeValues : { 
     ':hkey' : year_val, 
     ':rkey' : title 
    } 
}; 

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

輸出: -

輸出不具有該屬性的數據類型。

GetItem succeeded: { 
    "Items": [ 
    { 
     "title": "The Big New Movie", 
     "yearkey": 2015, 
     "info": { 
     "rating": 0, 
     "plot": "Nothing happens at all." 
     } 
    } 
    ], 
    "Count": 1, 
    "ScannedCount": 1 
}