How can I pass a collection name from query parameters successfully?

I am trying to pass a collection name through query parameters, but I’m facing issues with the response. When I use the URL /find?table=data, the code provided below correctly returns ‘data’ as a string when querying query.table. However, when I attempt to assign the collection name using this query parameter, it returns an empty list []. Even if I hardcode the collection name as “data” and keep the URL as /find?table=data, it still returns an empty list.

Endpoint: /find?table=data

Function:

javascriptCopy code

exports = async function({ query, headers, body }, response) {
    
      //return [query.table, Type of query.table ]  -- [ "data", "string"]

      if (query?.['_id'])  query._id = new BSON.ObjectId(query._id);

      const collection = context.services.get("mongodb-atlas").db("test").collection(query.table); //even with "data"
      return collection.find(query).toArray()
        .then(docs => {
          return JSON.parse(JSON.stringify(docs));
        })
    }
};

Output: []

This might sound stupid, but does the user or service you’re doing have permissions in proper permissions in place?

Thank you @Brock_Leonard for your reply.

I need to remove the ‘table’ key before I send it for query filtering, actually.

      const table = query.table; 
      delete query.table;
2 Likes

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