How to get object from POST request object using Angular Ajax call in NODEJS?

  • After clicking the button, the method is called. The code is there,

        .controller('templeDetailsList', function ($scope, $http, $ionicModal) {
    
         $scope.starclick = function(){
    
         var newFav = [{
                "favid":"4654646",
                "favname":"sometext"
            }];
    
         var favurl = "http://localhost:1337/users/adduser";
    
         $.post(favurl, newFav, function () {
                alert("Successfully posted data");
            });
        }
    
        });
    
          

  • Node service with mongodb code,

        var express = require('express');
    
        var router = express();
    
        router.post('/adduser', function (req, res) {
    
        var db = req.db;
    
        var d1 = req.body;
    
        var collection = db.get('userlist');
        collection.insert(req.body, function (err, result) {
            res.send(
                (err === null) ? { msg: '' } : { msg: err }
            );
        });
        });
    
          

    Http request worked successfully to call a service method. But I cannot get the request object in the service method. What can I do for this? I want to get a request for the data of an object in a service method. please help me.

+3


source to share


1 answer


Add body parser to your express app.



var router = express();
bodyParser = require('body-parser');
router.use(bodyParser.json());

      

0


source







All Articles