Why my Async and Await still not do the job of sequence execution
I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do
I'm passing values to a function and I'm expecting it to work like
Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.
But the result I see is
Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query
How do I achieve desired output from this.
var someFunction = async (a,b)=>
var k;
try
k = a+b;
catch(err)
return(err);
var document;
try
document = await db.collection(somecollection).findOne(_id:k)
catch(err)
return(err);
return(document);
someFunction(4,5).then((result)=>
console.log(result);
);
javascript angularjs node.js async-await
add a comment |
I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do
I'm passing values to a function and I'm expecting it to work like
Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.
But the result I see is
Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query
How do I achieve desired output from this.
var someFunction = async (a,b)=>
var k;
try
k = a+b;
catch(err)
return(err);
var document;
try
document = await db.collection(somecollection).findOne(_id:k)
catch(err)
return(err);
return(document);
someFunction(4,5).then((result)=>
console.log(result);
);
javascript angularjs node.js async-await
Which database are you using? Are you sure thatdb.collection(somecollection).findOne(_id:k)
returns a promise?
– jfriend00
Nov 15 '18 at 3:01
add a comment |
I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do
I'm passing values to a function and I'm expecting it to work like
Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.
But the result I see is
Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query
How do I achieve desired output from this.
var someFunction = async (a,b)=>
var k;
try
k = a+b;
catch(err)
return(err);
var document;
try
document = await db.collection(somecollection).findOne(_id:k)
catch(err)
return(err);
return(document);
someFunction(4,5).then((result)=>
console.log(result);
);
javascript angularjs node.js async-await
I'm a newbie to Node.js and not sure if I understood Async/Await correctly. Here's what I'm trying to do
I'm passing values to a function and I'm expecting it to work like
Step 1. add both values
Step 2. Fetch a document from db based on value from step 1
Step 3. return the document fetched from step 2.
But the result I see is
Step 1. add both values
Step 2. return undefined while db query is running
step 3. finish running query
How do I achieve desired output from this.
var someFunction = async (a,b)=>
var k;
try
k = a+b;
catch(err)
return(err);
var document;
try
document = await db.collection(somecollection).findOne(_id:k)
catch(err)
return(err);
return(document);
someFunction(4,5).then((result)=>
console.log(result);
);
javascript angularjs node.js async-await
javascript angularjs node.js async-await
asked Nov 15 '18 at 1:59
DavidDavid
106
106
Which database are you using? Are you sure thatdb.collection(somecollection).findOne(_id:k)
returns a promise?
– jfriend00
Nov 15 '18 at 3:01
add a comment |
Which database are you using? Are you sure thatdb.collection(somecollection).findOne(_id:k)
returns a promise?
– jfriend00
Nov 15 '18 at 3:01
Which database are you using? Are you sure that
db.collection(somecollection).findOne(_id:k)
returns a promise?– jfriend00
Nov 15 '18 at 3:01
Which database are you using? Are you sure that
db.collection(somecollection).findOne(_id:k)
returns a promise?– jfriend00
Nov 15 '18 at 3:01
add a comment |
1 Answer
1
active
oldest
votes
findone
may return undefined if no match found
findOne returns undefined on the server
In the first try...catch
block, why do you worry about an exception of adding two variables? that never result on error in javascript.
Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id
and I don't think that can be returned from adding a + b
This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 '18 at 2:28
try to cast k to ObjectId, addvar ObjectId = require('mongodb').ObjectId
in top and thenawait db.collection(somecollection).findOne(_id: new ObjectId(k) )
– doc_id
Nov 15 '18 at 4:47
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%2f53311362%2fwhy-my-async-and-await-still-not-do-the-job-of-sequence-execution%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
findone
may return undefined if no match found
findOne returns undefined on the server
In the first try...catch
block, why do you worry about an exception of adding two variables? that never result on error in javascript.
Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id
and I don't think that can be returned from adding a + b
This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 '18 at 2:28
try to cast k to ObjectId, addvar ObjectId = require('mongodb').ObjectId
in top and thenawait db.collection(somecollection).findOne(_id: new ObjectId(k) )
– doc_id
Nov 15 '18 at 4:47
add a comment |
findone
may return undefined if no match found
findOne returns undefined on the server
In the first try...catch
block, why do you worry about an exception of adding two variables? that never result on error in javascript.
Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id
and I don't think that can be returned from adding a + b
This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 '18 at 2:28
try to cast k to ObjectId, addvar ObjectId = require('mongodb').ObjectId
in top and thenawait db.collection(somecollection).findOne(_id: new ObjectId(k) )
– doc_id
Nov 15 '18 at 4:47
add a comment |
findone
may return undefined if no match found
findOne returns undefined on the server
In the first try...catch
block, why do you worry about an exception of adding two variables? that never result on error in javascript.
Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id
and I don't think that can be returned from adding a + b
findone
may return undefined if no match found
findOne returns undefined on the server
In the first try...catch
block, why do you worry about an exception of adding two variables? that never result on error in javascript.
Also make sure you're passing the _id correctly to findone, Mongo uses UUID to calculate _id
and I don't think that can be returned from adding a + b
edited Nov 15 '18 at 2:18
answered Nov 15 '18 at 2:12
doc_iddoc_id
890931
890931
This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 '18 at 2:28
try to cast k to ObjectId, addvar ObjectId = require('mongodb').ObjectId
in top and thenawait db.collection(somecollection).findOne(_id: new ObjectId(k) )
– doc_id
Nov 15 '18 at 4:47
add a comment |
This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 '18 at 2:28
try to cast k to ObjectId, addvar ObjectId = require('mongodb').ObjectId
in top and thenawait db.collection(somecollection).findOne(_id: new ObjectId(k) )
– doc_id
Nov 15 '18 at 4:47
This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 '18 at 2:28
This is an example I've taken here in real the first try will have much more to do. in the second block document exists but takes time to run the query, if I print the document in second block it prints moments later undefined is printed which shows that return statement is executed before the second block is finished executing.
– David
Nov 15 '18 at 2:28
try to cast k to ObjectId, add
var ObjectId = require('mongodb').ObjectId
in top and then await db.collection(somecollection).findOne(_id: new ObjectId(k) )
– doc_id
Nov 15 '18 at 4:47
try to cast k to ObjectId, add
var ObjectId = require('mongodb').ObjectId
in top and then await db.collection(somecollection).findOne(_id: new ObjectId(k) )
– doc_id
Nov 15 '18 at 4:47
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%2f53311362%2fwhy-my-async-and-await-still-not-do-the-job-of-sequence-execution%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
Which database are you using? Are you sure that
db.collection(somecollection).findOne(_id:k)
returns a promise?– jfriend00
Nov 15 '18 at 3:01