Getting Failed to set response in Data API. Request made from GAS

Greetings,

I have a Google App Script Code that connects to my Mongo DB for a Web app. However, when the post request is sent to findone it actually returns a null and the error in the log is “failed to set response” on Mongodb Logs.

Any help would be appreciated in helping me understand why i cannot seem to make the call to load the information.

The document that is pulled is quite a long array of data. Not sure if that is the reason or if there is a limit of what can be pulled.

> const findEndpoint = 'https://data.mongodb-api.com/app/XXXXX/endpoint/data/v1/action/findOne';
const clusterName = "ClusterNAME"
 
function lookupInspection() {

 const apikey = 'API KEY'

 const query = { edate: '2022_09_27'}
 //We can Specify sort, limit and a projection here if we want
 const payload = {
  filter: query, 
  collection: "COLNAME", database: "DB NAME", dataSource: clusterName
 }
 
 const options = {
  method: 'post',
  contentType: 'application/json',
  accept: 'application/json',  // THIS WAS ADDED TO SEE IF IT NEEDED TO ACCEPT RESPONSE AS WELL. 
  payload: JSON.stringify(payload),
  headers: { "api-key": apikey },
  muteHttpExceptions: true
 };
 
 const response = UrlFetchApp.fetch(findEndpoint, options);
 Logger.log(response.getContentText())
 const documents = JSON.parse(response.getContentText()).documents

 Logger.log(documents)
 

}

I’ve also run into the same issue recently. My script worked fine until a few months ago and I believe it has something to do with the query taking too long. Limiting the query to 100 items got my function to return the data properly but any more or if I try to sort the query will result in a “Error 500” on the App Scripts side, and “failed to set response” error in Mongo DB’s logs.

Not sure if this helps, but I would love to hear workarounds or updates to this problem since I have a lot more than 100 items to fetch.

Greetings,

the way i solved the issue was to convert the mongo db via a Python script using Pymongo. then I turned that script into a heroku webapp as a Flask API. then i made the calls from GAS to that API that i made. I was able to return everything that I needed and it was smooth as fast.
Hope that it helps.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.