-1

.controller("addContact",function($scope,$http){ $scope.add=function(){

    var myContact={
        "department":$scope.department,
        "name":$scope.name,
        "model":$scope.model,
        "year":$scope.year,
        "price":$scope.price
    };
    console.log(myContact);
    $http.post("/addItem",myContact)
        .then(function(response){
        console.log(response);
    });

public.post('/addItem',function(request,response){ console.log("incoming data=" +request.body);

fs.readFile(__dirname + "/public/items.json", 'utf-8', function(err, data) {
    console.log('file data' + data);
    var fileJsonData = data;
    var newContact = request.body;
    newContact.id = fileJsonData.items.length + 1;
    console.log("newContact=" + JSON.stringify(newContact));
    fileJsonData.items.push(newContact);
    var stringFile = JSON.stringify(fileJsonData);
    fs.writeFile(__dirname + "/items.json", stringFile);
    response.send(stringFile);

});

});

1 Answers1

0

Please check if any of your interceptors are causing this issue. Also check the status of the response to check if it makes sense.

This post might help you Angular.js $http intercept "net::ERR_CONNECTION_REFUSED"

Take some time to read the document for $http.post as I think you are not using it the correct way

Community
  • 1
  • 1
CrazyMac
  • 462
  • 4
  • 19