This issue is complicated, and I don't pretend to understand all the subtleties of what's going on here. In my app I'm using a customSchema to access the database with Mongoose directly. It's been working fine, until I tried adding compound indexes for a schema, and it started failing in really mysterious way (basically as if the object literal that was passed in as the index specifier was an empty object but it isn't).
After several hours of tracing through code, I think what's happening is that Railway is running the schema.js file in a new Node context, and the object literal defining the index fields is added to Mongoose at that point. However, later when the Mongoose call to the native driver's ensureIndex actually fires, it's happening in a different context, and the object literal index specifier no longer works (specifically, the driver checks that the literal's constructor property equals Object, but the check fails). I believe it's the same issue described:
I'm not sure what the solution is here.
Thanks. I'll report it to the Mongoose project too, see if they want to do anything on their side.