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);
}
}