I have something similar. Unfortunately the documentation is not very detailed and Realm still appears kind of Beta-ish to me. I am not sure if my solution is something Mongo advises. They seem bent on pushing GraphQL, but I want a “RESTful” API.
Instead of a Real, “Function”, here is what I have been doing
Select “3rd Partly Services”
Click the “Add Service” button.
Select HTTP and give it a name (RESTService)
Click the “Add incoming webHook” button
Chose an HTTP Method you wish to expose (i.e. GET)
Click the Function Editor Tab
Write you function to extract the data you need
To execute this webHook, copy webhookUrl and add your parms
Use the browser, Postman, Fiddler or whatever gives you the most pleasure
Note: You can add authentication, but that’s a whole different topic
Here is an example function I wrote to extract data:
exports = async function getStudents(payload, response) {
const {accountId } = payload.query;
const db = context.services.get(“mongodb-atlas”).db(“studentsDB”);
const students = db.collection(“students”);
console.log(getStudents accountId:${accountId}
);
const query = { accountId:accountId };
const projection = { "_id": 1, "studentId":"$_id", "firstName":"$firstName","lastName":"$lastName",
"accountId":"$accountId"};
response.setHeader("Content-Type","application/json");
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}}`);
})
}
“3rd Party Service” is a misnomer in my suggestion because your Mongo DB instance is the 3rd Party.
Hope this helps
You are not actually calling a 3rd Party Service, but instead