Read DB function - execution order
I am reading a DB and output the result in a string.
The issue is that sometimes the output is empty, my guess is that the function "json" is executed before the DB-read is completed.. I tried to apply "callback" which I read in other threads, but it didnt solve it.
What am I doing wrong?
var response = ;
var items = ;
var table = "table";
exports.handler = async (event) =>
ReadDB(json);
return response;
;
//**********************ReadDB Function
function ReadDB(callback)
var paramsRead =
TableName: table,
;
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
//**********************json Function
function json(items)
console.log(items);
response =
body: JSON.stringify(items, null, 't'),
;
console.log(response);
javascript node.js
add a comment |
I am reading a DB and output the result in a string.
The issue is that sometimes the output is empty, my guess is that the function "json" is executed before the DB-read is completed.. I tried to apply "callback" which I read in other threads, but it didnt solve it.
What am I doing wrong?
var response = ;
var items = ;
var table = "table";
exports.handler = async (event) =>
ReadDB(json);
return response;
;
//**********************ReadDB Function
function ReadDB(callback)
var paramsRead =
TableName: table,
;
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
//**********************json Function
function json(items)
console.log(items);
response =
body: JSON.stringify(items, null, 't'),
;
console.log(response);
javascript node.js
add a comment |
I am reading a DB and output the result in a string.
The issue is that sometimes the output is empty, my guess is that the function "json" is executed before the DB-read is completed.. I tried to apply "callback" which I read in other threads, but it didnt solve it.
What am I doing wrong?
var response = ;
var items = ;
var table = "table";
exports.handler = async (event) =>
ReadDB(json);
return response;
;
//**********************ReadDB Function
function ReadDB(callback)
var paramsRead =
TableName: table,
;
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
//**********************json Function
function json(items)
console.log(items);
response =
body: JSON.stringify(items, null, 't'),
;
console.log(response);
javascript node.js
I am reading a DB and output the result in a string.
The issue is that sometimes the output is empty, my guess is that the function "json" is executed before the DB-read is completed.. I tried to apply "callback" which I read in other threads, but it didnt solve it.
What am I doing wrong?
var response = ;
var items = ;
var table = "table";
exports.handler = async (event) =>
ReadDB(json);
return response;
;
//**********************ReadDB Function
function ReadDB(callback)
var paramsRead =
TableName: table,
;
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
//**********************json Function
function json(items)
console.log(items);
response =
body: JSON.stringify(items, null, 't'),
;
console.log(response);
javascript node.js
javascript node.js
asked Nov 14 '18 at 10:57
MartikMartik
98212
98212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Yes, This is called asynchronious. This is the relevant bit:
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
You likely expect that ddb.scan()
fires and completes, then callback()
. But the scan function is async, which (simply put) it doest block the remaining code.
You should read this code as: ddb.scan()
is started and then then callback()
is started. The .scan()
is practically always slower, thus no result.
In your example, you have the data in your else, this is where you trigger callback(data.Item)
, as that code is on the revolve of the .scan()
function.
Promises are a tricky concept to wrap your head around in the beginning, I suggest you do some research. Controling promises is difficult in the beginning, but understanding the basics is doable.
Suggested reading material I found by Googling "javascript simple promises explanation"
Thank you for your great reply! I have tried to put callback(items) under else-section, but this cause the "return response" to execute and return an empty json?
– Martik
Nov 14 '18 at 11:17
Same issue as explained above, but then withReadDB
andreturn
:) You could add a "async" to the ReadDB function, and an await in the call. Thats also someyou you can Google.
– Martijn
Nov 14 '18 at 11:42
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%2f53298583%2fread-db-function-execution-order%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
Yes, This is called asynchronious. This is the relevant bit:
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
You likely expect that ddb.scan()
fires and completes, then callback()
. But the scan function is async, which (simply put) it doest block the remaining code.
You should read this code as: ddb.scan()
is started and then then callback()
is started. The .scan()
is practically always slower, thus no result.
In your example, you have the data in your else, this is where you trigger callback(data.Item)
, as that code is on the revolve of the .scan()
function.
Promises are a tricky concept to wrap your head around in the beginning, I suggest you do some research. Controling promises is difficult in the beginning, but understanding the basics is doable.
Suggested reading material I found by Googling "javascript simple promises explanation"
Thank you for your great reply! I have tried to put callback(items) under else-section, but this cause the "return response" to execute and return an empty json?
– Martik
Nov 14 '18 at 11:17
Same issue as explained above, but then withReadDB
andreturn
:) You could add a "async" to the ReadDB function, and an await in the call. Thats also someyou you can Google.
– Martijn
Nov 14 '18 at 11:42
add a comment |
Yes, This is called asynchronious. This is the relevant bit:
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
You likely expect that ddb.scan()
fires and completes, then callback()
. But the scan function is async, which (simply put) it doest block the remaining code.
You should read this code as: ddb.scan()
is started and then then callback()
is started. The .scan()
is practically always slower, thus no result.
In your example, you have the data in your else, this is where you trigger callback(data.Item)
, as that code is on the revolve of the .scan()
function.
Promises are a tricky concept to wrap your head around in the beginning, I suggest you do some research. Controling promises is difficult in the beginning, but understanding the basics is doable.
Suggested reading material I found by Googling "javascript simple promises explanation"
Thank you for your great reply! I have tried to put callback(items) under else-section, but this cause the "return response" to execute and return an empty json?
– Martik
Nov 14 '18 at 11:17
Same issue as explained above, but then withReadDB
andreturn
:) You could add a "async" to the ReadDB function, and an await in the call. Thats also someyou you can Google.
– Martijn
Nov 14 '18 at 11:42
add a comment |
Yes, This is called asynchronious. This is the relevant bit:
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
You likely expect that ddb.scan()
fires and completes, then callback()
. But the scan function is async, which (simply put) it doest block the remaining code.
You should read this code as: ddb.scan()
is started and then then callback()
is started. The .scan()
is practically always slower, thus no result.
In your example, you have the data in your else, this is where you trigger callback(data.Item)
, as that code is on the revolve of the .scan()
function.
Promises are a tricky concept to wrap your head around in the beginning, I suggest you do some research. Controling promises is difficult in the beginning, but understanding the basics is doable.
Suggested reading material I found by Googling "javascript simple promises explanation"
Yes, This is called asynchronious. This is the relevant bit:
// Call DB to read the item from the table
ddb.scan(paramsRead, function(err, data)
if (err)
console.log("Error reading DynamoDB", err);
else
items = data.Items;
);
callback(items);
You likely expect that ddb.scan()
fires and completes, then callback()
. But the scan function is async, which (simply put) it doest block the remaining code.
You should read this code as: ddb.scan()
is started and then then callback()
is started. The .scan()
is practically always slower, thus no result.
In your example, you have the data in your else, this is where you trigger callback(data.Item)
, as that code is on the revolve of the .scan()
function.
Promises are a tricky concept to wrap your head around in the beginning, I suggest you do some research. Controling promises is difficult in the beginning, but understanding the basics is doable.
Suggested reading material I found by Googling "javascript simple promises explanation"
answered Nov 14 '18 at 11:01
MartijnMartijn
12.1k32055
12.1k32055
Thank you for your great reply! I have tried to put callback(items) under else-section, but this cause the "return response" to execute and return an empty json?
– Martik
Nov 14 '18 at 11:17
Same issue as explained above, but then withReadDB
andreturn
:) You could add a "async" to the ReadDB function, and an await in the call. Thats also someyou you can Google.
– Martijn
Nov 14 '18 at 11:42
add a comment |
Thank you for your great reply! I have tried to put callback(items) under else-section, but this cause the "return response" to execute and return an empty json?
– Martik
Nov 14 '18 at 11:17
Same issue as explained above, but then withReadDB
andreturn
:) You could add a "async" to the ReadDB function, and an await in the call. Thats also someyou you can Google.
– Martijn
Nov 14 '18 at 11:42
Thank you for your great reply! I have tried to put callback(items) under else-section, but this cause the "return response" to execute and return an empty json?
– Martik
Nov 14 '18 at 11:17
Thank you for your great reply! I have tried to put callback(items) under else-section, but this cause the "return response" to execute and return an empty json?
– Martik
Nov 14 '18 at 11:17
Same issue as explained above, but then with
ReadDB
and return
:) You could add a "async" to the ReadDB function, and an await in the call. Thats also someyou you can Google.– Martijn
Nov 14 '18 at 11:42
Same issue as explained above, but then with
ReadDB
and return
:) You could add a "async" to the ReadDB function, and an await in the call. Thats also someyou you can Google.– Martijn
Nov 14 '18 at 11:42
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%2f53298583%2fread-db-function-execution-order%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