mr.loop
mr.loop

Reputation: 1005

How to call a Mongodb Realm function from nodejs

I have a function deployed in Mongodb Realm that I want to call in my local nodejs app. I am able to connect to the Mongodb Atlas Cluster via the uri and MongoClient.

But they are only able to query the database and not call that function. Is there a way I can call that function using this connected instance of MongoClient.

I also discovered two npm packages - realm and realm-web. But I am unable to get the work done with either of them.

in realm-web, the docs says to use import like

import * as Realm from "realm";

but this doesn't work in node.

SyntaxError: Cannot use import statement outside a module

then in realm, the method to open the realm apparantly has two arguments

const Realm = require("realm");

const realm = Realm.open({
    path: "myrealm",
    schema: [TaskSchema],
  });

and I am unable to find what to fill in them.

Any suggestion or help is appreciated

Upvotes: 3

Views: 1139

Answers (1)

emragins
emragins

Reputation: 5167

You need to have a user object in order to call a function.

At a bare minimum, something like:

const credentials = Realm.Credentials.anonymous();
const user = await app.logIn(credentials);
const result = await user.functions.myFunction(arg1, arg2);

With this, you'll need to enable anonymous authentication (or a different auth provider for something more secure) and ensure that the function isn't private.

Referencing the docs:

Getting a user object: https://docs.mongodb.com/realm/sdk/node/examples/authenticate-users/

Calling a function: https://docs.mongodb.com/realm/sdk/node/examples/call-a-function/

Upvotes: 3

Related Questions