MongoDB Realm Functions on React Native

Viewed 131

Just started using the Realm MongoDB and i watched this video https://www.youtube.com/watch?v=Evp3xTzWCu4 from MongoDB and followed exactly what he did, but for some the function on the client side is not working. I'm using Expo React Native

I have this simple Realm function

exports = function(arg){

  var collection = context.services.get("mongodb-atlas").db("questiondb").collection("questions");
    collection.insertOne({name:arg}).then((doc) => {
      console.log('Success')
    }).catch(error=>console.log(error))
};

When i call it in the real console, it works fine.

This is the front end function

  const connectDB = async () => {
    const appID = "myapp-ckwfl";
    const app = new Realm.App({ id: appID });
    const credentials = Realm.Credentials.anonymous();
    try {
      const user = await app.logIn(credentials);
      await user.functions.addQuestion("Myself");

      console.log("Logged in");
    } catch (error) {
      console.log(error);
    }
  };

I'm getting the 'Logged in' in the console.

I went to check the activity log on the MongoDB atlas and it shows OK to both login and function

enter image description here

However, the function log shows me this message

[   "FunctionError: can't find a table mapping for namespace questiondb.questions" ] {   "name": "addQuestion" }

And i have the database 'questiondb' with the collection 'questions'.

What am i missing here?

1 Answers

I ran into a similar error. The problem was that my BSON did not contain an "_id" field. But the BSON validation when saving it allowed me to save the schema like that. But when querying data through graphql I got this exact same error. So the solution was to fix the BSON schema. Even if the BSON schema saves and deploys successfully it can still be that it will not work for graphql.

You can see if your BSON has errors by navigating here: enter image description here

Related