mongodbrealmmongodb-realm

MongoDB Realm: environment value exists but is undefined inside Realm Function


I am referencing an environment value from a Realm function as instructed here: context.values.get("appTwilioNumber")

I confirmed appTwilioNumber exists in our project: appTwilioNumber variable is defined for all environments and that our project is assigned an environment: enter image description here

Yet, when I call console.log('twilioNumberForEnv:', context.values.get("appTwilioNumber")); in our Realm function, I get twilioNumberForEnv: undefined.

EDIT 1: I have more info now--I logged out and logged back in (in case of multi-user sync issues), then exported my app from the Realm UI and the values folder is empty. Not sure if that is a separate bug, but updating in case this info is useful.

EDIT 2: the environment values are stored under environment, not under values. Edit 1 was a red herring. I do see appTwilioNumber in the exported app, but it still returns undefined in the Realm functions.


Solution

  • Wow... Mongo's documentation might be off.

    In another function, I found this: context.environment.values.anotherEnvValue instead of context.values.get("appTwilioNumber") . So I updated my code to context.environment.values.appTwilioNumber, and it worked.

    I did a CMD-f on both https://docs.mongodb.com/realm/values-and-secrets/define-environment-values/ and https://docs.mongodb.com/realm/values-and-secrets/access-a-value/ for ".environment", and it isn't on either page.

    I'll follow up with Mongo, but please use context.environment.values.YOURENVVALUE instead of context.values.get("YOURENVVALUE") if you encounter this issue.