I am trying to set up a MongoDB Atlas trigger function that searches a mongoDB database to collect string variables, then uses those as the to log into a separate API. When I run this function:
exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const EventData = mongodb.db("SIoT").collection("EventData");
var data = EventData.find({"_id":new BSON.ObjectId("XXXXXXXX")})
return data
};
It returns:
EJSON.parse('[{"_id":{"$oid":"XXXXXXXXX"},"CodeRequestAuthorization":"XXXXXXXXXXX","DataRequestAuthorization":"XXXXXXXXXX"}]')
But I am trying to save the CodeRequestAuthorization value as a variable in the function.
Feels like an incredibly simple problem but I have been stuck for about 5 hours.
Managed to solve the problem myself:
exports = async function(){
const EventData = context.services.get("mongodb-atlas").db("SIoT").collection("EventData");
const request = await EventData.findOne({"_id":new BSON.ObjectId("XXXXXXXXXXX")});
var data = request.XXXXXXX;