AWS DynamoDB 解析返回数据
AWS DynamoDB parsing returned data
我正在尝试使用JS查询DynamoDB并解析返回的数据。我必须承认我是 JavaScript 的新手,但我有一些奇怪的行为。
在下面的函数中,我传递了一个日期数组,并从我的 table
中检索对象
var queryDynamo = function(dateArray){
console.log(dateArray)
for (var i = 0; i < dateArray.length; i++) {
var params = {
TableName : "myTable",
KeyConditionExpression: "#day = :st ",
ExpressionAttributeNames:{
"#day": "day"
},
ExpressionAttributeValues: {
':st': dateArray[i]
}
};
var resp = docClient.query(params, function(err, data) {
if (err) {
console.log("ERR:"+JSON.stringify(err, undefined, 2))
} else {
data.Items.forEach(function(element) {
console.log(element)
});
}
});
}
console.log(resp.response)
return;
}
--> 下面是输出
constructor {request: constructor, data: null, error: null, retryCount: 0, redirectCount: 0, …}
data:
Count: 4
Items: (4) [{…}, {…}, {…}, {…}]
ScannedCount: 4
__proto__: Object
error: null
httpResponse: constructor {statusCode: 200, headers: {…}, body: Uint8Array(1134), streaming: false, stream: i, …}
maxRedirects: 10
maxRetries: 10
nextPage: ƒ (e)
redirectCount: 0
request: constructor {domain: undefined, service: t.c…r.t.constructor, operation: "query", params: {…}, httpRequest: constructor, …}
retryCount: 0
__proto__: Object
查询成功但是结果有点奇怪
resp.response 正确包含 data
对象,但我无法访问它。它说它是 null
,但显然不是,因为它有 4 个项目。
有什么想法吗?
您正试图在响应数据存在之前打印它。您的 console.log(resp.response)
行在 DynamoDB 查询完成且其结果已解组之前正在执行。这是异步 JavaScript.
中的 common gotcha
在 AWS.Request 对象中查看响应数据的一种方法是等待它,就像这样(尽管在 JavaScript 中您通常不会这样做):
var req = docClient.query(params, function(err, data) {
// as before: handle err, data
)};
setTimeout(function () {
console.log('Response data:', JSON.stringify(req.response.data));
}, 2000);
一种更常见的模式是使用 SDK 方法的承诺变体,如下所示:
docClient.query(params).promise()
.then(data => doSomething(data))
.catch(err => logError(err));
我正在尝试使用JS查询DynamoDB并解析返回的数据。我必须承认我是 JavaScript 的新手,但我有一些奇怪的行为。
在下面的函数中,我传递了一个日期数组,并从我的 table
中检索对象var queryDynamo = function(dateArray){
console.log(dateArray)
for (var i = 0; i < dateArray.length; i++) {
var params = {
TableName : "myTable",
KeyConditionExpression: "#day = :st ",
ExpressionAttributeNames:{
"#day": "day"
},
ExpressionAttributeValues: {
':st': dateArray[i]
}
};
var resp = docClient.query(params, function(err, data) {
if (err) {
console.log("ERR:"+JSON.stringify(err, undefined, 2))
} else {
data.Items.forEach(function(element) {
console.log(element)
});
}
});
}
console.log(resp.response)
return;
}
--> 下面是输出
constructor {request: constructor, data: null, error: null, retryCount: 0, redirectCount: 0, …}
data:
Count: 4
Items: (4) [{…}, {…}, {…}, {…}]
ScannedCount: 4
__proto__: Object
error: null
httpResponse: constructor {statusCode: 200, headers: {…}, body: Uint8Array(1134), streaming: false, stream: i, …}
maxRedirects: 10
maxRetries: 10
nextPage: ƒ (e)
redirectCount: 0
request: constructor {domain: undefined, service: t.c…r.t.constructor, operation: "query", params: {…}, httpRequest: constructor, …}
retryCount: 0
__proto__: Object
查询成功但是结果有点奇怪
resp.response 正确包含
data
对象,但我无法访问它。它说它是null
,但显然不是,因为它有 4 个项目。有什么想法吗?
您正试图在响应数据存在之前打印它。您的 console.log(resp.response)
行在 DynamoDB 查询完成且其结果已解组之前正在执行。这是异步 JavaScript.
在 AWS.Request 对象中查看响应数据的一种方法是等待它,就像这样(尽管在 JavaScript 中您通常不会这样做):
var req = docClient.query(params, function(err, data) {
// as before: handle err, data
)};
setTimeout(function () {
console.log('Response data:', JSON.stringify(req.response.data));
}, 2000);
一种更常见的模式是使用 SDK 方法的承诺变体,如下所示:
docClient.query(params).promise()
.then(data => doSomething(data))
.catch(err => logError(err));