0

I have the following part in a nodejs file

client.db('metro4').collection('Station').findOne({"Name": variabled2}, function(err, res2) {
        if(err) throw err;
        variabled2 = res2._id;
        console.log("id2 = ", res2._id);
    });

console.log("v= ",variabled2);

myFunc(variabled);

the myFunc function also has some mongodb queries.

The problem is in the order of execution.

v = undefined
then some of function results (which are obviously wrong)
id2 = 4
then other of functions results

I came from running MySQL queries inside node and there weren't these problems even in long stored procedures.

Now many answers suggested 'async and await'. So, I made myFunc async & tried this

(async function() {
        await recur2();
    })();

But as expected the result are nearly same as the program is not waiting for first query.

Also, I discovered .then() like Promises but they apparantly don't work when having callback function like I have in my query above.

Thanks in advance.

1
  • Please add full example of your asym/await function. It seems you are trying to mix Promises and callbacks, which is quite advanced technique. mongodb.github.io/node-mongodb-native/4.3/classes/… without callback parameter returns a Promise. You should use this syntax with async/await. Remember async/await is just syntax sugar on top of Promises. Commented Jan 11, 2022 at 11:17

1 Answer 1

1

The first query is using callback so it won't wait for its completion. So what you need is await the first query, or place myFunc into the callback.

place inside callback

let variabled2;
client.db('metro4').collection('Station').findOne({"Name": variabled2}, 
    function(err, res2) {
        if(err) throw err;
        variabled2 = res2._id;
        console.log("id2 = ", res2._id);

        myFunc(variabled);
    });

async/await

(async function() {
    let variabled2;
    const res2 = await client.db('metro4').collection('Station').findOne({"Name": variabled2})
    variabled2 = res2._id;
    console.log("id2 = ", res2._id);
    myFunc(variabled);
})()

I would suggest the async/await solution as it is cleaner.

Sign up to request clarification or add additional context in comments.

7 Comments

SyntaxError: await is only valid in async functions and the top level bodies of modules. I guess the function in which query is made should also be async for this
@mr.loop Oh sorry I assumed that you are running inside a function. Yes you can simply wrap it inside an async function. Just edited the answer.
the nested callbacks work but in (async function() { await recur2(); console.log("Hello") })(); Here, Hello is being consoled before the completion of recur2. Don't know why. recur2 is a async recursive (with mongo queries) function.
Well, that should works fine and I guess it is about not awaiting inside recur2(), i.e. using then or callback. I cannot tell what's exactly going on as I don't know what's inside recur2(), but if you print something on the very first of recur2(), that should go before Hello.
Yeah I figured await was not used in recursion in that function. The probelm I am encountering is async function recur() {if { // } else { await recur() } } it says await only valid in async functions, but recur is an async function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.