After spending some time searching I couldn’t find a clear way forward so here goes:
I will have a realm trigger querying my Atlas db daily.
I want to send the results of the trigger to my backend node server, (currently running on my local machine → will be eventually deployed, not sure where yet).
Any idea on how to send the results to my backend?
Humm I think you are doing this the other way around. When the backend node server needs the data, he can consume a REST (webhook) or a GraphQL API for example to retrieve the data it needs.
I guess you could also send a POST command to your backend though with the data when it’s ready.
In this scenario, you don’t need a webhook. You just execute a function with the trigger that sends a POST command to your backend which needs to be listening, of course.
However, when trying to send an array, it’s sending just an empty response.
When I return the array to the console, it’s full. Any clue what’s going here?
exports = function() {
const mongodbAtlas = context.services.get("mongodb-atlas");
const auctions = mongodbAtlas.db("myFirstDatabase").collection("items");
//Date now
var now = new Date();
//Get customer with last bid
const findHighestBidder = auctions.aggregate([
{$match: { $and: [
{endDate: {$lt: now}},
{status: "active" }
] }},
{ $project : { status: 1, bidHistory: 1 } },
{$addFields : {bidHistory : {$reduce : {
input : "$bidHistory",
initialValue : {bid : 0},
in : {$cond: [{$gte : ["$$this.bid", "$$value.bid"]},"$$this", "$$value"]}}
}}}
])
const result = findHighestBidder.toArray
//This returns the results as expected
return result
//This returns: "{ results: {} }"
return context.http.post({
url: "http://27b1e5a30df2.ngrok.io/api/stripe/test",
body: {result} ,
encodeBodyAsJSON: true
})
};
I think you have a couple of issue in the above piece of code.
toArray is a function so you need the parenthesis:
const result = findHighestBidder.toArray()
toArray() returns a promise, not the actual result. See this example. So currently, your variable result that you use to populate your body doesn’t contain what you think it contains.