Using JS SDK in Twilio Node - writing to private filesystem error?

I am using the Realm JS SDK in the ‘Function’ (Node 12) Environment of Twilio.

When i use the function for the first time i get the following error …

UnhandledPromiseRejectionWarning: Unhandled promise rejection: Error: make_dir() failed: Read-only file system Path: /var/task/mongodb-realm/
Exception backtrace: /var/task/node_modules/realm/compiled/napi-v4_linux_x64/realm.node(ZN5realm4util4File11AccessErrorC1ERKSsS4+0x2d) [0x7fd2bb1a654d]
/var/task/node_modules/realm/compiled/napi-v4_linux_x64/realm.node(_ZN5realm4util12try_make_dirERKSs+0x166) [0x7fd2bb298ef6]
/var/task/node_modules/realm/compiled/napi-v4_linux_x64/realm.node(+0x4bbebd…

Subsequent calls all work fine and i can use the function multiple times without any issues.
I guess its trying to write to a private filesystem for some reason - for the first time ??

Here is the code …

exports.handler = async function(context, event, callback) {
  
  console.log('require realm');
  const Realm = require("realm");
  
  console.log("event", event);
  
  const user = event.from;
  console.log("user", user);
  const enquiry = event.enquiry
  const body = event.body
  
  // NBP01
  const app = new Realm.App({ id: "nbp01-*****" });
  const apiKey = "*************************";

  const timestamp = new Date();
  const status = "twilio";
  const easyid = "E0010";
  const code = "action";
  const userid = user
  const device = "twiliofunction"
  const datastr = userid + enquiry
  const datadoc = { "enquiry": enquiry, "body": body }
  const theArgs = [timestamp, status, easyid, code, userid, device, datastr, datadoc];
  
  if (!apiKey) {
    throw new Error("Could not find a Realm Server API Key.");
  }
  
  const credentials = Realm.Credentials.serverApiKey(apiKey);
  
  try {
    const user = await app.logIn(credentials);
    console.log("Successfully logged in!", user.id);
    
    console.log("Calling Realm Function");
    const result = await user.callFunction("sevent", theArgs)
    console.log("Function result", result);
    user.logOut
    return callback(null, result);

  } catch (err) {
    console.error("Failed to log in", err.message);
    return callback(error);
  }

}
1 Like

The error is occurring here …
const app = new Realm.App({ id: “nbp01-*****” });

Hi @Damian_Raffell, what happens if you try to write directly to /var/task/mongodb-realm/ (just to confirm that this is the issue)?

Looks like you can only write to the tempdir in a Twilio Function: Utilize Temporary Storage to Read & Write in a Twilio Function. Maybe try setting Realm.defaultPath to somewhere in your function’s tempdir

I figured that the same issue might exist for Lambda functions and a quick search came up with this… Realm not working in AWS Lambda (serverless framework)

Hi Andrew … thanks for helping out.

Yep … i found most of that info too…

Strange thing is that if i catch the first error and just run it again - it works ok !
There must be some shonkey code in there ? :slight_smile:

  try {
    app = new Realm.App({ id: id });
  } catch(err) {
    console.log("Building Realm.App failed with : ", err.message);
    app = new Realm.App({ id: id });
    console.log("Built Realm.App again!");
  }

The Twilio Function only calls a Realm Function (no DB work).
I will try setting Realm.defaultPath … but i think it wants a fully qualified path ?