Tuesday 30 May 2017

MEAN stack post method doesnt work

I was working on some basic angular 1.6 CRUD app with express and mongo and i have a problem with POST method. In my browser's console it returns an error like 'XML didn't find main element'(its in polish) and in express console it throws undefined

here's my code

angular

 var myApp = angular.module('myApp', []);
 myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http){

$http({
    method: 'GET',
    url: '/contactList'
}).then(function(response){
    console.log('i got it')
    $scope.contactList = response.data;
})


$scope.addContact = function(){
    console.log($scope.contact)
    $http.post('/contactList').then(function(request){
        $scope.contact = {}
    })
}
}])

express

const express = require('express')
mongojs = require('mongojs')
bp = require('body-parser')
db = mongojs('contactList', ['contactList'])
app = express();


app.use(express.static(__dirname + "/src"))

app.use(bp.json())
app.use(bp.urlencoded({ extended: true }));

app.get('/contactList', (req, res)=> { 

console.log('i recived the get request'); 

db.contactList.find((err, docs)=>{
    console.log(docs)
    res.json(docs)
})
})

app.post('/contactList', (req, res)=> {
    console.log(req.body.desc);
    res.end();
})

app.listen(3000);

console.log('server runnong on port 3000')



via EDGECRUSHER

No comments:

Post a Comment