I recently started working with Realm and decided to write my schemas on Realm’s UI, essentially using the JSON Schema Editor. Prior to using Realm, I used to do this validation through mongoose on a dedicated server, using javascript. However, given Realm’s cloud functions, and serverless nature, I opted for just creating the JSON schema on Realm’s UI.
However, when I attempt to update or create a document using a cloud function, and schema validation fails, I get an empty error object, with no information on where or why it failed Schema validation.
This seemed interesting, because the collection insert, update, etc, functions do create a writeError
object, when they fail, according to MongoDB’s documentation
Note: the reason why I want the writeError
Object info, is that debugging is easier, and I can chain cloud functions together, and create error stack traces.
Below is small example of my dilemma:
Schema:
"bsonType": "object",
"properties": {
"_id": {
"bsonType": "string"
},
"account_type": {
"bsonType": "string",
"enum": [
"student",
"teacher",
"admin"
],
"maxLength": 300,
"minLength": 0
},
Sample Cloud Function:
export = async function (){
const collection = context.services.get("mongodb-atlas").db("Development").collection("users");
const user_id = context.user.id;
try {
//failing condition since account_type cannot be parent
const result = await collection.insertOne({_id: user_id, account_type: "parent"});
return result
} catch (e) {
const error = new Error ( "could not create a new user")
error.metadata = e
return error
}
}
Returned Error:
So my question is:
Can I get a more detailed error message back? If so, what should I add/change to get this writeError
object?