In order to make function dynamic, I am try to read collection names from headers but getting this error. I have create custom header key namely ‘table’ and gave collection name as value.
context.services.get("mongodb-atlas").db("XYZ").collection(headers.table.toString()).insertOne(JSON.parse(body.text()));
TypeError: Cannot access member 'toString' of undefined
1 Like
Hey @Rajan_Braiya,
Thank you for reaching out to the MongoDB Community forums.
Could you please clarify how you are passing the collection name and provide the full code snippet to better understand the issue?
However, if you want to make the function dynamic and pass the collection name in the Atlas Function, you can refer to the following code snippet:
exports = async function (coll_name) {
const result = context.services.get("mongodb-atlas").db("test").collection(coll_name).findOne();
return {result}
}
In this case, you need to pass the coll_name
from the Testing Console
as shown in the screenshot below:
I hope it helps!
Regards,
Kushagra
Hi @Kushagra_Kesav Thank you for your reply.
I am trying to test API using Postman with the static collection name its working fine, but I want to read collection name from headers object to make endpoint more dynamic for all table. So I tried creating new key on postman headers namely ‘table’ and in the value I am passing the actual collection name, but when I am read like headers.table
or headers.table.toString()
getting error.
Hello @Kushagra_Kesav,
I’m attempting to call MongoDB endpoints from an Angular web application, and everything seems to be working well. However, I need to pass the collection name dynamically with the API request. To achieve this, I’m trying to send it within the headers by adding a custom header named ‘Table’ to hold the collection name.
Here is the JavaScript function:
find(id, collection): Observable<any> {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `civcw ywewe2422`,
'Table': collection
});
const queryParams = new URLSearchParams(id ? { _id: id } : {});
return this.http.get(`${mongo}/find?${queryParams}`, { headers: headers });
}
In my MongoDB function:
context.services.get("mongodb-atlas").db("xyz").collection(headers.Table.toString());
When I tested this using Postman, it worked perfectly. However, when I make the same call from the web app, I encounter a CORS policy error. I even tried making the call without the custom header, and it worked fine on the app.
Here’s the CORS error message:
Access to XMLHttpRequest at 'https://eu-west-1.aws.data.mongodb-api.com/app/zyx-xshsy/endpoint/find?' from origin 'http://localhost:4200' has been blocked by the CORS policy. The response to the preflight request doesn't pass the access control check, as there is no 'Access-Control-Allow-Origin' header present on the requested resource.
GET https://eu-west-1.aws.data.mongodb-api.com/app/xyz-xshsy/endpoint/find? net::ERR_FAILED
It seems like the issue lies with CORS (Cross-Origin Resource Sharing) policy blocking your request when it's coming from the web app at 'http://localhost:4200'. You may need to configure CORS settings on the server-side to allow requests from this origin.```