2017-05-28 111 views
-1

所以我有一個10個項目的表格,每個項目有大約5個鍵(名稱,經驗,水平等)。現在,我想掃描該表,將每個項目作爲對象並將其添加到數組,然後JSON對該數組進行字符串化並將其返回。如何掃描lambda中的dynamoDB?

我只需要掃描代碼的幫助,並獲取所有項目並將其放入數組中。

這是我目前的代碼。

var dynamodb = new AWS.DynamoDB.DocumentClient(); 
exports.handler = function(event, context, callback) 
{ 

    var returnArray = { 
     "cards": {} 
    } 
    getCards(); 

    function getCards() {//Not sure how to write this function 

     var params = { 
      TableName : "toBeApprovedTable", 
      Key: {//not sure what to put here, since I want all items, and not searching through keys. 
      }, 
     }; 

     dynamodb.scan(params,function(err,data) 
     { 
      if(err) 
      { 
       console.log("error in scanning"); 
      } 
      else 
      { 
       console.log("scanning success!"); 
       //Not sure what to do here. 
      } 
     }); 
    } 

}; 

回答

0

我在淘汰Google + AWS文檔後發現它。

以下是如何掃描表中所有元素的表格。我的返回是一張地圖,其中包含一組元素。每個元素都是我的對象的地圖。

exports.handler = function(event, context, callback) 
{ 

    var returnArray = { 
     "cardsToBeApproved":[] 
    }; 
    getCards(); 

    function getCards() {//Not sure how to write this function 

     var params = { 
      TableName : "toBeApprovedTable" 
     }; 

     dynamodb.scan(params,onScan); 
    } 

    function onScan(err,data) 
    { 
     if(err) 
     { 
      console.log("unable to scan table"); 
     } 
     else 
     { 
      console.log("scan succeeded"); 
      data.Items.forEach(function(card) 
      { 
       console.log("my card name is " + card.name); 
       var cardStringified = JSON.stringify(card); 
       returnArray.cards.push(card); 
      }); 
      callback(null,JSON.stringify(returnArray)); 
     } 
    } 

}; 
+0

放下評論,如果你需要我解釋一行:) – TheQ