2017-04-17 27 views
0

我正在使用DocumentClient(http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html)来更轻松地使用DynamoDB。但是,它好像在使用Date对象时遇到问题。我知道DynamoDB需要格式化为特定日期的日期Date (as ISO8601 millisecond-precision string, shifted to UTC)AWS DynamoDB DocumentClient可以处理本机JavaScript Date对象吗?

是否DocumentClient不处理这个问题,或者是否有需要在Date对象上设置的东西?

现在,我刚刚通过toString()将该值转换为字符串。 expires_at的值具体为:

这个不包含expires_at在DyanmoDB Item中。

{ 
    Item: 
    { 
    id: 'session', 
    credentials: 
    { 
     access_token: '', 
     refresh_token: '', 
     token_type: 'Bearer', 
     expires_in: 3599, 
     expires_at: 2017-04-17T18:48:03.608Z 
    } 
    }, 
    TableName: 'table' 
} 

而这其中将包括它:

{ 
    Item: 
    { 
    id: 'session', 
    credentials: 
    { 
     access_token: '', 
     refresh_token: '', 
     token_type: 'Bearer', 
     expires_in: 3599, 
     expires_at: 'Mon Apr 17 2017 18:50:24 GMT+0000 (UTC)' 
    } 
    }, 
    TableName: 'table' 
} 

回答

0

DocumentClient仅仅是DynamoDB的抽象层。因此,如果DynamoDB不支持Date数据类型,则它将不受DocumentClient支持。 (请参阅DynamoDB Data Types

您可以做的是使用toISOString()方法通过ISO 8601字符串。例如:

var expires = new Date(); 
expires.setTime(expires.getTime() + (60*60*1000)); // Add 1 hour. 

{ 
    Item: 
    { 
    id: 'session', 
    credentials: 
    { 
     access_token: '', 
     refresh_token: '', 
     token_type: 'Bearer', 
     expires_in: 3599, 
     expires_at: expires.toISOString() 
    } 
    }, 
    TableName: 'table' 
} 
相关问题