Unable to use "async" or "await" in a Scheduled Trigger

Hello,

I am trying to build my very first Scheduled Atlas trigger and I am experiencing some difficulties:

When starting a new trigger, there will be some commented example code provided by MongoDB inside the text area where you are supposed to write your trigger code. What immediately surprised me was that that example code did not contain await. For instance, this is what MongoDB uses as an example:

const doc = collection.findOne({ name: "mongodb" });

Why are these official query examples not including await?

When trying to write something similar I was receiving an empty object {} as a response.

I have then added async to the function as well as added await to the find query and it worked, I can see data now when console.log’ing it. However, the trigger function is now giving me the following warning:

image

Is there a way to correct this within the trigger?

Thank you!

Hello :wave: @Vladimir,

Welcome to the MongoDB Community forums :sparkles:

Thank you for highlighting this. We have taken note of it and will let the concerned team know. However, please note that the example snippet is intended for reference only and can be modified to suit the user’s specific use case.

I am unable to find anything similar to what you are referring to. Would you please be able to share the complete code snippet with us?

Best,
Kushagra

1 Like

Hi @Kushagra_Kesav ,
I also had the same problem as above. I have a function that needs it to run on a schedule and I found a scheduled trigger
here is the code i want to convert it on Imgur: The magic of the Internet and you will see “async” or “await”. It helps me to process this function on this line when it’s done, the system will let me run the next function on the next line to explain why I need to do it. I would appreciate you very much if you have some ideas or solutions for this. I look forward to hearing your thoughts. Thank you so much in advance

Hey @Phan_Thanh_Lam_N_A,

Apologies for the late response.

I tried to create a trigger using the code you provided, and it worked fine for me. I didn’t encounter any errors related to 'async functions' is only available in es8 (use 'esversion: 8'). If you’re experiencing an error message, could you please share the exact message or provide more details about the specific issue you’re encountering while creating the trigger?

Additionally, if you’re facing challenges with using async/await, you can consider using promises as a workaround.

Could you please elaborate or give an example of the scenario that you have in mind?

Best,
Kushagra