Why my Async and Await still not do the job of sequence execution
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 3:01
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 1:59
David
106
106
Which database are you using? Are you sure thatdb.collection(somecollection).findOne({_id:k})
returns a promise?
– jfriend00
Nov 15 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 at 3:01
Which database are you using? Are you sure that
db.collection(somecollection).findOne({_id:k})
returns a promise?– jfriend00
Nov 15 at 3:01
Which database are you using? Are you sure that
db.collection(somecollection).findOne({_id:k})
returns a promise?– jfriend00
Nov 15 at 3:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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 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 at 4:47
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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 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 at 4:47
add a comment |
up vote
1
down vote
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 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 at 4:47
add a comment |
up vote
1
down vote
up vote
1
down vote
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 at 2:18
answered Nov 15 at 2:12
doc_id
874930
874930
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 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 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 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 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 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 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 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 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 at 3:01