Connection String for Realm might not be working with NodeJS Driver

I wanted to connect to a Realm-enabled database with mongo:// url in typescript (NodeJS). I wrote like:

  const apiKey = process.env.REALM_API_KEY;
  const appId = process.env.REALM_APP_ID;
  const mongoUrl = `mongodb://_:${apiKey}@realm.mongodb.com:27020/?authMechanism=PLAIN&authSource=%24external&appName=${appId}:mongodb-atlas:api-key&ssl=true`;
  console.log(mongoUrl);
  const client = await MongoClient.connect(mongoUrl, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

This produces an error:

MongoServerSelectionError: failed to handle command request "ismaster": error processing client metadata: expected app name to be composed of: <appID>:<svc>:<authProvider>, got [object Object]

I understand there is remoteMongoClient to do the similar thing, but I want to know whether mongodb:// url can be used as well.

What I did:

  • checked ‘MongoDB Connection String’ is enabled in Realm UI Linked Data Sources section
  • tried both with and without ?ssl=true (the error message changes though)
  • checked console.log(mongoUrl) to see the url is properly provided
  • tried the same url in mongo command (mongo "mongodb://......") and see it works in the shell
  • put the same url in Compass to connect, and got the same error

Why I wanted:

  • I wanted to try out 3rd party ORM libraries while utilize Realm’s great user/role system. Those libraries often require the mongodb:// url to initialize connection.

I suspect this is a “Realm ↔ NodeJS Driver” combination problem. (So I edited the title)

Does NodeJS Driver properly understand the colon-separators in appName of the mongo URI?

1 Like

I reposted this in the Driver category as it seems more relevant.