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: []