Atlas triggers, not working with async await

I want to firstly check the difference between atlas triggers and realm-triggers, as the docs for atlas triggers now quote realm.

however i have atlas collection insert trigger which works fine, however when i try to update my code with async await, and run the code, the result is not as expected.

for example i am using a package that returns a promise (bluebird)
and when i await xxxx
the result is {“isFulfilled”:false,“isRejected”:false}
while i am not getting promise pending, nor the actual result, the above result shows me that the result has not yet returned even though i am using async await.

if i replace await with

.then((resp) => {
console.log(resp)
}

this works, and the result is shown.

so the problem is with async await… are there any issues with async await with mongo db atlas triggers?

1 Like

Hi @Rishi_uttam ,

First the Atlas triggers are based on Realm triggers. In fact it creates a dedicated realm app for you behind the scenes for convenience.

Now to use async in a trigger function you need to define it in the header of the function.

The below article has some examples:
Triggers Treats and Tricks: Cascade Document Delete Using Triggers Preimage | MongoDB

Thanks
Pavel

Hi Pavel, Thanks

Yes i thought that behind the scens it is using realm triggers.

The main problem is async await, i do know how to use it, and i do put async in the code. The problem is i am using a package called ‘clearbit’ which i have uploaded as a dependency. When i await clearbit the result shows as : [{“isFulfilled”:false,“isRejected”:false}]"

But when use a normal .then , .catch, the code works as expected.

When testing on my local node machine, i found that async /await works fine, its only with mongodb triggers with the clearbit package async await does not work… – do you have any ideas? i know the package is using bluebird and needle under the hood to return the promise.

1 Like

Can you please navigate to your Realm trigger in the UI and copy paste me the link in the browser :slight_smile:

I’m experiencing the same issue. I have a function that uses Twilio and I can’t get it to work with async/await but I can with then

Works

exports = (payload) => {
  const client = require("twilio")
  client.doSomething()
      .then(( x) => { return x.status })  
      .catch(( error ) => { return error })  
};

this does not work

exports = async (payload) => {
  const client = require("twilio")
  cons x = await client.doSomething()
  return x.status || "pls work"
};

Am I missing something obvious?

cons x = await client.doSomething()

Maybe the cons typo …

Nope. Maybe I’m just really missing something obvious here?

  exports = async (payload) => {
    
    const { phone, code } = payload;
    
    const phoneWithPlus = `+${phone}`
    
    const client = require('twilio')(context.values.get("TWILIO_ACCOUNT_SID_VALUE"), context.values.get("TWILIO_AUTH_TOKEN_VALUE"));

    const verification_check = await client.verify.v2.services(context.values.get("TWILIO_VERIFY_SERVICE_SID_VALUE"))
      .verificationChecks.create({to: phoneWithPlus, code: code})
    
    if (verification_check.status == "approved") {
       return phone
    }
    
    return null
    
  };

I’m having the same issue when using node-geocoder. Works fine on my machine, I can seemingly get a response with .then and log it, but with async/await it’s totally busted.

E.g…

exports = async function (query) {
  const NodeGeocoder = require("node-geocoder");

  return {
    query,
    locations: await NodeGeocoder({
      provider: 'virtualearth',
      apiKey: context.values.get('virtualEarthKey')
    }).geocode(query)
  }
}
1 Like

Thanks for the great feedback.

An investigation ticket was open with our team.

Hopefully we will have answers soon.

Meanwhile, use promises where async/await cause trouble …

Appropriate you patience :wink:

Pavel

Any updates on this?