AWS DynamoDB parsing returned data
I am trying to use JS to query DynamoDB and parse the returned data. I must admit that I am new to JavaScript but I am having some weird behaviours.
In the following function I am passing an array of dates and I am retrieving objects from my 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;
--> The following is the output
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
The query succeeds but the result is kind of weird.
resp.response correctly contains the
data
object but I cannot access it. It says that it'snull
while it clearly is not since it has 4 Items.Any thoughts?
javascript arrays amazon-web-services amazon-dynamodb dynamodb-queries
add a comment |
I am trying to use JS to query DynamoDB and parse the returned data. I must admit that I am new to JavaScript but I am having some weird behaviours.
In the following function I am passing an array of dates and I am retrieving objects from my 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;
--> The following is the output
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
The query succeeds but the result is kind of weird.
resp.response correctly contains the
data
object but I cannot access it. It says that it'snull
while it clearly is not since it has 4 Items.Any thoughts?
javascript arrays amazon-web-services amazon-dynamodb dynamodb-queries
The DocumentClient methods return an AWS.Request object. The response data will be contained in an AWS.Response object (which is the context of the callback function) and also in the data parameter passed to your callback.
– jarmod
Nov 13 '18 at 18:34
data.data.Items.forEach(function(element) {
might work.
– Will
Nov 13 '18 at 18:53
add a comment |
I am trying to use JS to query DynamoDB and parse the returned data. I must admit that I am new to JavaScript but I am having some weird behaviours.
In the following function I am passing an array of dates and I am retrieving objects from my 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;
--> The following is the output
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
The query succeeds but the result is kind of weird.
resp.response correctly contains the
data
object but I cannot access it. It says that it'snull
while it clearly is not since it has 4 Items.Any thoughts?
javascript arrays amazon-web-services amazon-dynamodb dynamodb-queries
I am trying to use JS to query DynamoDB and parse the returned data. I must admit that I am new to JavaScript but I am having some weird behaviours.
In the following function I am passing an array of dates and I am retrieving objects from my 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;
--> The following is the output
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
The query succeeds but the result is kind of weird.
resp.response correctly contains the
data
object but I cannot access it. It says that it'snull
while it clearly is not since it has 4 Items.Any thoughts?
javascript arrays amazon-web-services amazon-dynamodb dynamodb-queries
javascript arrays amazon-web-services amazon-dynamodb dynamodb-queries
edited Nov 13 '18 at 18:45
isherwood
37.2k1082111
37.2k1082111
asked Nov 13 '18 at 18:06
MatteoMatteo
31
31
The DocumentClient methods return an AWS.Request object. The response data will be contained in an AWS.Response object (which is the context of the callback function) and also in the data parameter passed to your callback.
– jarmod
Nov 13 '18 at 18:34
data.data.Items.forEach(function(element) {
might work.
– Will
Nov 13 '18 at 18:53
add a comment |
The DocumentClient methods return an AWS.Request object. The response data will be contained in an AWS.Response object (which is the context of the callback function) and also in the data parameter passed to your callback.
– jarmod
Nov 13 '18 at 18:34
data.data.Items.forEach(function(element) {
might work.
– Will
Nov 13 '18 at 18:53
The DocumentClient methods return an AWS.Request object. The response data will be contained in an AWS.Response object (which is the context of the callback function) and also in the data parameter passed to your callback.
– jarmod
Nov 13 '18 at 18:34
The DocumentClient methods return an AWS.Request object. The response data will be contained in an AWS.Response object (which is the context of the callback function) and also in the data parameter passed to your callback.
– jarmod
Nov 13 '18 at 18:34
data.data.Items.forEach(function(element) {
might work.– Will
Nov 13 '18 at 18:53
data.data.Items.forEach(function(element) {
might work.– Will
Nov 13 '18 at 18:53
add a comment |
1 Answer
1
active
oldest
votes
You are attempting to print the response data before it exists. Your console.log(resp.response)
line is executing before the DynamoDB query has completed and its results have been unmarshalled. This is a common gotcha in asynchronous JavaScript.
One way to see the response data in the AWS.Request object is to wait for it, like this (though you would never typically do this in 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);
A more common pattern is to use the promise variants of the SDK methods, like this:
docClient.query(params).promise()
.then(data => doSomething(data))
.catch(err => logError(err));
Hello, thank you very much for your reply. I actually tried and I get results. I admit that I did not know about asynchronous behaviours in JS, my bad. I will have a read to the link you provided my. On the other hand, if you have any suggestions please do let me know because I am keen to learn how to do proper javascript code (I am a self learner so I would like not to learn "the bad way")
– Matteo
Nov 13 '18 at 21:14
Async JavaScript is challenging, not least because there are numerous ways to write async code (the mainstream options are callbacks, promises, and async/await). Here are a couple of videos that attempt to describe all three: youtube.com/watch?v=PoRJizFvM7s and youtube.com/watch?v=gB-OmN1egV8
– jarmod
Nov 13 '18 at 22:16
Thanks! I'll have a look
– Matteo
Nov 13 '18 at 23:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287058%2faws-dynamodb-parsing-returned-data%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are attempting to print the response data before it exists. Your console.log(resp.response)
line is executing before the DynamoDB query has completed and its results have been unmarshalled. This is a common gotcha in asynchronous JavaScript.
One way to see the response data in the AWS.Request object is to wait for it, like this (though you would never typically do this in 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);
A more common pattern is to use the promise variants of the SDK methods, like this:
docClient.query(params).promise()
.then(data => doSomething(data))
.catch(err => logError(err));
Hello, thank you very much for your reply. I actually tried and I get results. I admit that I did not know about asynchronous behaviours in JS, my bad. I will have a read to the link you provided my. On the other hand, if you have any suggestions please do let me know because I am keen to learn how to do proper javascript code (I am a self learner so I would like not to learn "the bad way")
– Matteo
Nov 13 '18 at 21:14
Async JavaScript is challenging, not least because there are numerous ways to write async code (the mainstream options are callbacks, promises, and async/await). Here are a couple of videos that attempt to describe all three: youtube.com/watch?v=PoRJizFvM7s and youtube.com/watch?v=gB-OmN1egV8
– jarmod
Nov 13 '18 at 22:16
Thanks! I'll have a look
– Matteo
Nov 13 '18 at 23:16
add a comment |
You are attempting to print the response data before it exists. Your console.log(resp.response)
line is executing before the DynamoDB query has completed and its results have been unmarshalled. This is a common gotcha in asynchronous JavaScript.
One way to see the response data in the AWS.Request object is to wait for it, like this (though you would never typically do this in 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);
A more common pattern is to use the promise variants of the SDK methods, like this:
docClient.query(params).promise()
.then(data => doSomething(data))
.catch(err => logError(err));
Hello, thank you very much for your reply. I actually tried and I get results. I admit that I did not know about asynchronous behaviours in JS, my bad. I will have a read to the link you provided my. On the other hand, if you have any suggestions please do let me know because I am keen to learn how to do proper javascript code (I am a self learner so I would like not to learn "the bad way")
– Matteo
Nov 13 '18 at 21:14
Async JavaScript is challenging, not least because there are numerous ways to write async code (the mainstream options are callbacks, promises, and async/await). Here are a couple of videos that attempt to describe all three: youtube.com/watch?v=PoRJizFvM7s and youtube.com/watch?v=gB-OmN1egV8
– jarmod
Nov 13 '18 at 22:16
Thanks! I'll have a look
– Matteo
Nov 13 '18 at 23:16
add a comment |
You are attempting to print the response data before it exists. Your console.log(resp.response)
line is executing before the DynamoDB query has completed and its results have been unmarshalled. This is a common gotcha in asynchronous JavaScript.
One way to see the response data in the AWS.Request object is to wait for it, like this (though you would never typically do this in 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);
A more common pattern is to use the promise variants of the SDK methods, like this:
docClient.query(params).promise()
.then(data => doSomething(data))
.catch(err => logError(err));
You are attempting to print the response data before it exists. Your console.log(resp.response)
line is executing before the DynamoDB query has completed and its results have been unmarshalled. This is a common gotcha in asynchronous JavaScript.
One way to see the response data in the AWS.Request object is to wait for it, like this (though you would never typically do this in 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);
A more common pattern is to use the promise variants of the SDK methods, like this:
docClient.query(params).promise()
.then(data => doSomething(data))
.catch(err => logError(err));
edited Nov 13 '18 at 23:29
answered Nov 13 '18 at 19:11
jarmodjarmod
19.5k64149
19.5k64149
Hello, thank you very much for your reply. I actually tried and I get results. I admit that I did not know about asynchronous behaviours in JS, my bad. I will have a read to the link you provided my. On the other hand, if you have any suggestions please do let me know because I am keen to learn how to do proper javascript code (I am a self learner so I would like not to learn "the bad way")
– Matteo
Nov 13 '18 at 21:14
Async JavaScript is challenging, not least because there are numerous ways to write async code (the mainstream options are callbacks, promises, and async/await). Here are a couple of videos that attempt to describe all three: youtube.com/watch?v=PoRJizFvM7s and youtube.com/watch?v=gB-OmN1egV8
– jarmod
Nov 13 '18 at 22:16
Thanks! I'll have a look
– Matteo
Nov 13 '18 at 23:16
add a comment |
Hello, thank you very much for your reply. I actually tried and I get results. I admit that I did not know about asynchronous behaviours in JS, my bad. I will have a read to the link you provided my. On the other hand, if you have any suggestions please do let me know because I am keen to learn how to do proper javascript code (I am a self learner so I would like not to learn "the bad way")
– Matteo
Nov 13 '18 at 21:14
Async JavaScript is challenging, not least because there are numerous ways to write async code (the mainstream options are callbacks, promises, and async/await). Here are a couple of videos that attempt to describe all three: youtube.com/watch?v=PoRJizFvM7s and youtube.com/watch?v=gB-OmN1egV8
– jarmod
Nov 13 '18 at 22:16
Thanks! I'll have a look
– Matteo
Nov 13 '18 at 23:16
Hello, thank you very much for your reply. I actually tried and I get results. I admit that I did not know about asynchronous behaviours in JS, my bad. I will have a read to the link you provided my. On the other hand, if you have any suggestions please do let me know because I am keen to learn how to do proper javascript code (I am a self learner so I would like not to learn "the bad way")
– Matteo
Nov 13 '18 at 21:14
Hello, thank you very much for your reply. I actually tried and I get results. I admit that I did not know about asynchronous behaviours in JS, my bad. I will have a read to the link you provided my. On the other hand, if you have any suggestions please do let me know because I am keen to learn how to do proper javascript code (I am a self learner so I would like not to learn "the bad way")
– Matteo
Nov 13 '18 at 21:14
Async JavaScript is challenging, not least because there are numerous ways to write async code (the mainstream options are callbacks, promises, and async/await). Here are a couple of videos that attempt to describe all three: youtube.com/watch?v=PoRJizFvM7s and youtube.com/watch?v=gB-OmN1egV8
– jarmod
Nov 13 '18 at 22:16
Async JavaScript is challenging, not least because there are numerous ways to write async code (the mainstream options are callbacks, promises, and async/await). Here are a couple of videos that attempt to describe all three: youtube.com/watch?v=PoRJizFvM7s and youtube.com/watch?v=gB-OmN1egV8
– jarmod
Nov 13 '18 at 22:16
Thanks! I'll have a look
– Matteo
Nov 13 '18 at 23:16
Thanks! I'll have a look
– Matteo
Nov 13 '18 at 23:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287058%2faws-dynamodb-parsing-returned-data%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The DocumentClient methods return an AWS.Request object. The response data will be contained in an AWS.Response object (which is the context of the callback function) and also in the data parameter passed to your callback.
– jarmod
Nov 13 '18 at 18:34
data.data.Items.forEach(function(element) {
might work.– Will
Nov 13 '18 at 18:53