MongoDB Atlas Function inconsistent behaviour between HTTPS call and debug run

My simple Atlas function is as follows:

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

  // const {id} = await query;
  const id = "63a18506eeb4aa7b878751b5"
  const collection = await context.services.get("mongodb-atlas").db("GatherDB").collection("Project")
  const project = await collection.findOne({_id:BSON.ObjectId(id)})
  
  return project.name
};

When I call this function (impersonating a user) using the “run” button on Atlas Function editor I get the expect response, a project name for the given Id.

When I call this function via Postman passing the user authentication in headers I get this error:

{
    "error": "cannot compare to undefined",
    "error_code": "FunctionExecutionError",
    "link": "https://realm.mongodb.com/groups/637360845e9d607c6236bea6/apps/6373656a5122b133c4f719f1/logs?co_id=63ce65fe10282834d993ce0a"
}

I’m struggling to understand this inconsistent behaviour.

Hi @Sam_Roberts - Welcome to the community :wave:

Just a few questions i’m hoping you can provide some details for:

  1. What is the authentication setting for the function? (Application Auth, System, User Id, Script)
  1. I’m trying to replicate this behaviour by calling the function using postman on my test environment. I have replicated a similar function that returns a single document. Just to clarify, have you created a HTTPS Endpoint which is linked to the function? If you can provide details on how you’re calling the function via postman to help reproduce this error, that would be good in identifying if the behaviour is expected or not.

Look forward to hearing from you.

Regards,
Jason

A simple case of not toggling “Fetch Custom User Data” on when using Application Auth. Apologies for wasting your time!!

1 Like

Thanks for updating the post with the resolution Sam :slight_smile:

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