How to send variable(mongodb doc id) from one async function to other async function in javascript?

I’ve two async functions, which are async function start(){} & async function end(){} So in both functions I’m having mongodb operations. I want to send document id generated inside start() function to the end() function and then log it there. This is my start function:

async function start() {    
      await client.connect();
      const db = client.db(dbName);
      const col = db.collection("mongotroncol");
      let personDocument = {
        "name" : "shubham"
       // Insert a single document, wait for promise so we can read it back
       const p = await col.insertOne(personDocument);
       const abc = p.insertedId;
       const docid = abc.toString()
       return docid;

and then I’ve this end() function and I’m trying to send unique docid generated in start() function to the end() function with returning that value & log it in end() function as follows:

async function end() {
      mongoid = await start();

So for example “623cb515366c70a2a3c27288” this is my document id generated in start() function then what I desire is to console.log this same id in end() function but as I’m writing await start() in end() function, again start() function is called & run, result of which new mongodb document id is generated and new one is logged but I want the doc id generated from start() only.

One important point: I’ve two events which are app.on(‘ready’) & app.on(‘quit’) so I’m calling await start() & await end() respectively like this:

app.on('ready', () => {
await start();


app.on('quit', () => {
await end();

So my 1st button click will trigger app.on(‘ready’) event which will run async start() function and generate a mongodb document with unique docid and then on my 2nd button click(which I’ll do after some minutes) will trigger app.on(‘quit’) which will run async end() function & ideally I want the same docid generated in async start() function but I’m getting “623cb515366c70a2a3c27289” which is exactly one bit incremented than the generated in start() function [please see the id value mentioned in the earlier paragraph] So the concern is to get same docid. Thank you!!

Hello @shubham_metkar
Welcome to the Community Forum!!

As per the above mentioned paragraph, the start function seems to be called twice, one on the two consecutive clicks as the await end() also calls await start() according to the function definition.

Hence, the expectation will be the console.log would log the latest doc_id which is different from the first doc_id created in the first function call.

Also, if the index is not created on name field, multiple entries with unique doc_id will be created.

Let us know if you need any further information.


Hello @Aasawari , thank you for the reply my only intention behind this whole execution is to get amount of time app is opened and sending it to the database so if I provide my desktop app(exe) to ‘n’ number of users I’ll be able to track amount of time my application was used by the user .
As you said correctly, as await start() is called once again it’ll again create new document & get the latest doc id, so I came with another better work around of localStorage/SessionStorage but as I’m using electron js, it doesn’t have inbuilt support for localStorage/SessionStorage, so I tried with some 3rd parties npm packages, doing this I was able to set the doc id but at the time of getting the id in my await end() function I was unable to retrieve it saying promise pending so right now last solution over this problem I could think is I’ll send 2 separate documents to the mongodb & on the basis of same[it is machine id with the help of node-machine-id package] key:value pair in both documents I would need to use aggregate function and get those documents with same machine id & then get the active app time(difference between start time & end time).

So this is the story so far, would like to hear your suggestions/idea on this…
Thank you,

Hi @shubham_metkar

The easiest approach I can think of is( which may not be an efficient way) would be to use a global variable to store the doc_id and pass the same id as parameter to the async end() function.

Also, this would also be one of an approach to find the counter value. I would suggest you to apply this approach or any efficient way you consider to be better and let us know if you see any issues related to MongoDB.

I would also recommend you to look for better approach over other forums (stack overflow) for better understanding.
Let us know if you have any further questions.

Happy coding!! :star_struck:


Hello @Aasawari , sorry for replying late, passing global id as a parameter to the end() function idea worked like a charm, thank you so much :smile:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.