I’m trying to build out a function that will accept a payload of IDs, query the DB and return related documents.
At the moment I have these values hardcoded in my function, but the client calling this API is getting an empty object.
My function is:
exports = async function (payload, response) {
const collection = context.services.get("mongodb-atlas")
.db("mydb")
.collection("my collection");
const foundWantedCards = await collection.find(
{ card_id: { $in: [ "61ddf66c68ebbba1a4084e65", "61ddf5b768ebbba1a4fd2c33" ] }, forTrade:"Yes" }
);
return foundWantedCards
}
When I run this function within the editor I get a result
> result:
[
{
"_id": {
"$oid": "61e995ff2738954edae011ec"
},
"card_id": "61ddf5b768ebbba1a4fd2c33",
...
which is correct, but the value of FoundWantedCards
is empty. How should I return the result in the reponse?
Also, how should I access the payload that is sent in the POST request? I’ve tried via EJSON.parse(payload.body.text())
but again that is empty.
Any help much appreciated!
OK, I am successfully getting a response back now, but I still can’t figure out how to access the payload in the POST request. payload
is truncated in my logs so I can’t see all the values, but payload.body
doesn’t show anything. I’ve tried looking in context
but can’t find anything either.
In my logs I do see an object
{
"name": "wantedCards",
"arguments": [
{
"query": {},
"headers": {
"Content-Length": [
"55"
],
"Sec-Ch-Ua-Mobile": [
"?0"
],
"User-Agent": [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.83 Safari/537.36"
],
"Origin": [
"http://localhost:3000"
],
"Sec-Fetch-Mode": [
"cors"
],
"Sec-Fetch-Dest": [
"empty"
],
"X-Envoy-External-Address": [
"51.194.154.211"
],
"Sec-Ch-Ua": [
"\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\""
],
"Content-Type": [
"application/json"
],
"Referer": [
"http://localhost:3000/"
],
"Accept-Encoding": [
"gzip, deflate, br"
],
"X-Cluster-Client-Ip": [
"51.194.154.211"
],
"X-Request-Id": [
"afc2ddf7-883a-4287-83f7-0b96e45bb098"
],
"Sec-Ch-Ua-Platform": [
"\"macOS\""
],
"Accept": [
"*/*"
],
"Sec-Fetch-Site": [
"cross-site"
],
"Accept-Language": [
"en-GB,en-US;q=0.9,en;q=0.8"
],
"X-Forwarded-For": [
"51.194.154.211"
],
"X-Forwarded-Proto": [
"https"
]
},
"body": "eyI2MWRkZjY2YzY4ZWJiYmExYTQwODRlNjUiOiI2MWRkZjViNzY4ZWJiYmExYTRmZDJjMzMifQ=="
},
{}
]
}
an I assume that the body
property here is the encoded payload, but how do I access this in my function?
@Stuart_Brown Did you solve that issue? I am having the same problem.
It’s been a while, but I think it was along the lines of:
exports = async function (payload, response) {
const collection = context.services.get("mongodb-atlas")
.db("cards")
.collection("my collection");
const data = JSON.parse(payload.body.text())
1 Like