0

Title says it all. I'm new to this so I'm sure it must be a simple mistake. Here's the controller

    $scope.removeProduct = function(product){
console.log(product._id);
    var inData = new Object();
    inData._id = product._id;
    console.log(inData);
    $http({ url:"/api/deleteprod/", inData, method: "POST"
            }).then(function () {
                        console.log("got here");
                        var index = $scope.vehicles.indexOf(product);
                        $scope.vehicles.splice(index, 1);

            })


};

and here's the server side.

module.exports = function(app, mongoose, config) {

    app.post('/api/deleteprod', function(req, res){
                    console.log("in app post",req);
    var MongoClient = mongodb.MongoClient;
    var url='mongodb://localhost:27017/seedsdev';
});
};

Obviously what I want is to pass the _id to the server so I can work with it, but when I output req it's about 50 pages long and has none of the info I wanted. Before it's passed the object can be seen to be fine fia console.log.

What's the rookie mistake I'm making?

2 Answers 2

2

When calling $http, you pass the post data with a data property. Currently you're passing an inData property. Change to this:

$http({ url:"/api/deleteprod/", data: inData, method: "POST" }).then(...)

Update:

On the server side, you'll need to make sure you have a JSON parsing middleware, like that from body-parser:

app.use(require('body-parser').json())

Once you are parsing the body using body-parser, you'll have a req.body property with the parsed JSON.

Sign up to request clarification or add additional context in comments.

4 Comments

Already tried that, doesn't work. I've been reading/trying tutorials for hours.
Can you elaborate on "doesn't work?" Is the POST not performed (check network tab in dev tools)? Is the server code not handling the request properly?
I still get 50 pages of gibberish in req. I tried outputing req.data and req.body as well but they came back undefined
Actually I'm an idiot. I just went to doublecheck and req.body works fine. Whoops.
1

What you are missing are two below things.

1) Data in post request as suggested by @Jacob

2) A parser of Post param body-parser. //npm install body-parser --save This will help you to parse the POST data in node js.

So code would look like

$scope.removeProduct = function(product){
console.log(product._id);
    var inData = new Object();
    inData._id = product._id;
    console.log(inData);
    $http({ url:"/api/deleteprod/", data: inData, method: "POST"
            }).then(function () {
                        console.log("got here");
                        var index = $scope.vehicles.indexOf(product);
                        $scope.vehicles.splice(index, 1);

            })


};

IN Backend

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
   module.exports = function(app, mongoose, config) {

        app.post('/api/deleteprod', function(req, res){
                        console.log("in app post",req.body._id);
        var MongoClient = mongodb.MongoClient;
        var url='mongodb://localhost:27017/seedsdev';
    });
    };

2 Comments

I think what I'm really missing is a brain, see the comments on the other post.
Hehe happy coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.