2016-07-20 32 views
1

我正在使用AWS.DynamoDB.DocumentClient和Dynamodb local(端口8080)。當我執行放置時,回調中的data變量是一個空對象。我錯過了什麼嗎?AWS.DynamoDB.DocumentClient未提供有關put的數據

import * as AWS from "aws-sdk"; 


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


const docClient: any = new AWS.DynamoDB.DocumentClient(); 


const item = { 
     someField: "456", 
     other:   "123" 
}; 

const params = { 
    TableName: "TableName", 
    Item: item 
}; 

docClient.put(params, function(err, data) { 
    if (err) console.log(err); 
    else console.log(data); // this produces: {} 
}); 

沒有錯誤,並且該項目被插入\更新 - 然而data變量是一個空對象。不應該包含值?

感謝

回答

1

您並沒有要求任何值回來,那麼爲什麼你認爲會有回來的任何值?如果您希望其中任何一個回到響應中,您需要設置適當的參數ReturnConsumedCapacity和/或ReturnItemCollectionMetrics和/或ReturnValues

+1

那麼get()返回插入的對象或其某些屬性的正確語法是什麼? –

+1

@DangerGinger查看DocumentClient頁面上的Parameters文檔以獲取詳細信息:http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property –

+1

@ MasonG.Zhwiti,我做了並最終找到解決方案。我搜索了這個問題,這個答案是一個很好的提示,但沒有提供OP的問題的具體解決方案。 –

0

如你不能「ReturnValues」實際返回任何有用的設置值,我的工作圍繞它通過簡單的回傳中傳遞原始項目數據。

import * as AWS from "aws-sdk"; 


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


const docClient: any = new AWS.DynamoDB.DocumentClient(); 


const item = { 
     someField: "456", 
     other:   "123" 
}; 

const params = { 
    TableName: "TableName", 
    Item: item 
}; 

// Have a clear reference to this scope 
var self = this; 

docClient.put(params, function(err, data) { 
    if (err) console.log(err); 
    else console.log(self.params.Item); // this should return the data you passed in! 
}); 

我設置爲「自「作爲」這個「來清楚說明我從哪裏讀取數據。希望能幫助別人!