Get response "FunctionExecutionError" from function

Hi,
Was wondering if someone could help me - I am trying to limit the fields returned through HTTP calls - in the function, I have put the following which I believe to be related the error I get the following error - “FunctionExecutionError”, Any guidance/help would be really appreciated.

exports = function({ query, headers, body }, response) {

const { nodeNameSearch, sortOn, sortByAscDesc, skipNumber, takeNumber, nodeFilter, collateralRequired } = query;
console.log("nodeNameSearch, sortOn, sortByAscDesc, skipNumber, takeNumber, nodeFilter, collateralRequired: ", nodeNameSearch, sortOn, sortByAscDesc, skipNumber, takeNumber, nodeFilter, collateralRequired);

var collection = context.services.get("mongodb-atlas")
    .db("Nodes")
    .collection("liveNodes");

var result = "";

if(nodeNameSearch != 'ALL'){
  // try only do a text search first
  result = collection.find({nodeID: {$regex : nodeNameSearch}},
  {
  title: 1, 
  nodeId: 1, 
  icon: 1, 
  nodeType: 1, 
  "nodePriceData.rewardedAsset": 1,
  "nodePriceData.collateral": 1,
  "nodePriceData.currentAssetPrice": 1,
  "nodePriceData.nodeValue": 1,
  "resources":1,
  "mediaLinks": {$slice: 5},
  "supportedOs":    1, 
  
}
  ).then(rslt => {
   rslt.supportedOs.forEach(function (item, index) {

Hi @pb665 and welcome in the MongoDB Community :muscle: !

Where is the rest of the function? Can you please fix the code block so it’s complete and correctly displayed?
I guess you noticed already but this forum supports standard markdown code blocks.

Here is an example of a custom function I’m using to implement a REST API. It’s old school because it’s still using the old 3rd Party Service that is now deprecated. So there may be a few details to change. But I think it’s just the function signature (params extraction). The interesting part is just the last 5 lines I guess for you.

function isPositiveInteger(str) {
    return ((parseInt(str, 10).toString() == str) && str.indexOf('-') === -1);
}

function log_ip(payload) {
  const log = context.services.get("pre-prod").db("logs").collection("ip");
  let ip = "IP missing";
  try {
    ip = payload.headers["X-Envoy-External-Address"][0];
  } catch (error) {
    console.log("Can't retrieve IP address.")
  }
  console.log(ip);
  log.updateOne({"_id": ip}, {"$inc": {"queries": 1}}, {"upsert": true})
    .then( result => {
      console.log("IP + 1: " + ip);
    });
}

exports = function(payload, response) {
  log_ip(payload);

  const {uid, country, state, country_iso3, min_date, max_date, hide_fields} = payload.query;
  const coll = context.services.get("mongodb-atlas").db("covid19").collection("global");
  
  var query = {};
  var project = {};
  const sort = {'date': 1};
  
  if (uid !== undefined && isPositiveInteger(uid)) {
    query.uid = parseInt(uid, 10);
  }
  if (country !== undefined) {
    query.country = country;
  }
  if (state !== undefined) {
    query.state = state;
  }
  if (country_iso3 !== undefined) {
    query.country_iso3 = country_iso3;
  }
  if (min_date !== undefined && max_date === undefined) {
    query.date = {'$gte': new Date(min_date)};
  }
  if (max_date !== undefined && min_date === undefined) {
    query.date = {'$lte': new Date(max_date)};
  }
  if (min_date !== undefined && max_date !== undefined) {
    query.date = {'$gte': new Date(min_date), '$lte': new Date(max_date)};
  }
  if (hide_fields !== undefined) {
    const fields = hide_fields.split(',');
    for (let i = 0; i < fields.length; i++) {
      project[fields[i].trim()] = 0
    }
  }
  
  console.log('Query: ' + JSON.stringify(query));
  console.log('Projection: ' + JSON.stringify(project));
  
  coll.find(query, project).sort(sort).toArray()
    .then( docs => {
      response.setBody(JSON.stringify(docs));
      response.setHeader("Contact","devrel@mongodb.com");
    });
};

Cheers,
Maxime.

Thanks for your reply and apologies for not texting the whole function and late response. I think the learning point for me is that MongoDB function does not support a then function or a foreach ?

I’m using “then” in my code block above, so it’s definitely supported.
Same for forEach. See the doc here for example.