Sure. Its a webhook in Realm.
I’m not actually using a Realm “Function” but a Realm 3rdParty HTTP webhook.
Here is the code:
/*
- getStudents webhook function request handler.
- Get a list of students for the given arguments
- arguments: accountId
- console execution ex: exports({query: {accountId:‘5e89f69d1c9d440000929b9a’ }}, new HTTPResponse())
*/
exports = async function getStudents(payload, response) {
const {accountId } = payload.query;
const db = context.services.get(“mongodb-atlas”).db(“usersDB”);
const students = db.collection(“students”);
console.log(getStudents accountId:${accountId});
const query = { accountId:accountId };
//const projection = { "studentId": "$_id", };
const projection = { "_id": 1, "studentId":"$_id", "firstName":"$firstName","lastName":"$lastName", "accountId":"$accountId"};
console.log ('getting students query:', JSON.stringify(query));
response.setHeader("Content-Type","application/json");
//await students.find(query).toArray()
await students.find(query, projection).toArray()
.then(result => {
if(result) {
response.setStatusCode(200);
//response.setBody(`{"students":${result}`);
response.setBody(`{"students":${JSON.stringify(result)}}`);
}
else {
console.log("students not found:",JSON.stringify(result));
response.setStatusCode(404);
response.setBody(`{message:"No students not found for given criteria"}`);
}
}).catch(err => {
console.log("error getting students:",err);
response.setStatusCode(500);
response.setBody(`{error:${err}}`);
})
}