Working with MongoDB Realm tutorial for Node js gives HTTP/1.1 401 Unauthorized error

I have been following the NodeJS MongoDB Realm Quick Start tutorial. The connection is made with the MongoDB Atlas and then immediately the app quits with

Logged in with user 5ff8c70ddd74f48bbe641a4c
Connection[1]: Session[1]: client_reset_config = false, Realm exists = true, async open = false, client reset = false
Connection[1]: Connected to endpoint '"remote-ip-here"' (from '192.168.1.212:60173')
ERROR: Connection[1]: Websocket: Expected HTTP response 101 Switching Protocols, but received:
HTTP/1.1 401 Unauthorized

I have enabled Realm Sync and enabled development mode and anonymous access.

My code is pretty much the same as mentioned in the tutorial:

export const realmApp = new Realm.App({ id: 'my-app-id' });
var realm: Realm;

async function run() {
  const credentials = Realm.Credentials.anonymous();
  await realmApp.logIn(credentials);
  console.log('Logged in with user', realmApp.currentUser?.id);
  realm = await Realm.open({
    schema: [TaskSchema],
    sync: {
      user: realmApp.currentUser as Realm.User,
      partitionValue: 'myPartition',
    },
  });
}

run().catch((err) => {
  console.error('Failed to open realm:', err);
});

Any ideas?

Sheikh,

Make sure that you have a sufficient Node.js version (> 10), see

https://docs.mongodb.com/realm/node/install

Then clear your local MongoDB Realm cache

rm -rf mongodb-realm

And try again.

Richard Krueger

Hi Richard thank you for the reply. I am using NodeJS version 12.20.1 so I do not think that is causing issues. I have an IoT app that collects data from some devices all day long. I am now using api keys in my app. The app does not error out as before immediately when the app is launched. But after some hours it crashes with the same error.

Connection[1]: Connected to endpoint '<remote-ip-here>:443' (from '192.168.8.105:41332')
ERROR: Connection[1]: Websocket: Expected HTTP response 101 Switching Protocols, but received:
HTTP/1.1 401 Unauthorized

I have tried removing the mongodb-realm folder twice now, but still the same issue occurs. Any further suggestions as to what might be actually happening here?