Insert data from another app to mongodb

Hello i have connected rest api service mongodb to my application. But i got problem with the http endpoints function. App that i talk about basically like digital form. after we fill in and click submit the data will be exported to database. But, While i do export data from my app form it works but only the id(autogenerated) that has been inserted to the database. The parameters such as namegoods, quantity and the rest is missing or not recorded.

Someone can explain this problem? did i declare a correct function to insert data from another app form?
My Function like picture below:

Hi @Faiq_Anargya ,

I am not sure what you are trying to achieve here and the insertOne method you wrote looks something like a mongoose schema defenition.

If I am reading correctly, your application will call an http endpoint passing the required new “goods” object as a body to that endpoint and the endpoint should store it in the database?

Correct me if I am wrong, in that case the function should like something like:

// This function is the endpoint's request handler.
exports = async function({ query, headers, body}, response) {
    // Data can be extracted from the request as follows:

    // Recieved data from the post request in "data" object
    const data = JSON.parse(body.text());

    const good = {
        _id : data._id,
        namegoods: data.namegoods,
        quantity:  data.quantity,
        condition: data.condition,
        barcode : data.barcode
    }

    const collection = await context.services.get("Syndes").db("Soti").collection("newgoods")
    
    const result = await collection.insertOne(good);

    return result;
};

Perhaps also consider to use the Atlas Data API instead of defining your own endpoints if the need is just to perform simple CRUD:

Thanks
Pavel

1 Like

Yeah sure, i want to insert a new data from my app and store it in the database.

i have tried your function but it shows the error:

"error": "{\"message\":\"Cannot access member 'text' of undefined\",\"name\":\"TypeError\"}",
    "error_code": "FunctionExecutionError",
    "link": "https://realm.mongodb.com/groups/630d71853be32c526f524ebe/apps/630d7789b5b628f76fcf2edd/logs?co_id=631764f0d84017ad42b4c8a3"
}

when i try to test in the postman

Can you show me the postman screen shot?

Also send me a link to your function in the cloud…

here it’s the postman screenshoot

and the link to of http ednpoints
https://data.mongodb-api.com/app/data-htvfp/endpoint/insert

Are you sending anything in the body?

That should be the document you want to insert …
I suspect that you have no body in that request and therefore the body.text() errors out…

And.by a link to the function I meant going in the web browser to the function page and copy paste here the URL that is in the browser when you are on that page. This way I can have a look…

Thank you very much for your help, it works when i try again. Actually, i’m very new with mongodb and this kind of things so i need help for some of the features and how it works

Sure no problem.

A good start will be to read some of the tutorials on our developer center:

In general browse through:

Thanks!

1 Like