Wednesday, 7 June 2017

Inserting Data into MONGODB with POST request

I'm using npm request to insert data into MongoDB - like a direct injection using the variables of a form. This is what I can see in the browser after I click the submit button: FORM DATA

And this is the script I'm using to insert credentials into the DB:

request.post(
            'http://127.0.0.1:8081/process_post', {
                json: {
                    fname: 'john',
                    surname:'smtith',
                    }
            },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    console.log(body)
                }
            }
        );

This the server side function:

app.post('/process_post', urlencodedParser, function (req, res) {
    var response = {fname: req.body.fname, surname: req.body.surname, age: req.body.age, 
password: req.body.password,email: req.body.email, phone: req.body.phone, 
city: req.body.city, country: req.body.country, postal_code: req.body.postal_code, 
description: req.body.description, picture:req.body.picture};

//etc

The issue is that the server gets all messed up with the variables and does a random match of the sent data. Also, in the mongo shell I can see that data comes as "undefined"...Is it because I'm sending a Json and the server side is set to capture variables? Anyhow, how can I fix this? Thanks



via Mellville

No comments:

Post a Comment