Slip
Slip

Reputation: 949

Why get not working?

I am working with Express and Angular, when I do a POST request to express from Angular it works fine

function ConditionsCreateController(conditionsService,$http) {
    var vm = this;
    vm.create = create;
    var ConditionsCreateForm;

    function create() {
        var request = $http.post('/conditions/', vm.ConditionsCreateForm);
        request.success(function (data) {
            console.log(data.msg);
        });
        request.error(function (data) {
            console.log(data.msg);
        });
    };
}

But when i try to do a GET request in my service

vm.get = function () {
    $http.get('/conditions/list').success(function(data) {
        alert(data);
    });
};

I got this error in my response:

GET http://localhost:3000/conditions/list 404 (Not Found)

My Express routing (conditions.js)

'use strict';

var express = require('express');
var mongoose = require('mongoose');
var Conditions = mongoose.model('Conditions');


var router = express.Router();
var Conditions = mongoose.model('Conditions');

 //Working
router.post('/', function (req, res) {   
    var condition = new Conditions({name:req.body.name});
    condition.save(function(err) {  
        if (err) throw err;
    });

    //Not working
    router.get('/list', function (req,res){
        Conditions.find({}, function (err, docs) {
            console.log(docs);
        });
    }); 
});
module.exports = router;

What am I missing?

Upvotes: 0

Views: 243

Answers (1)

jfriend00
jfriend00

Reputation: 707318

Making my comment into an answer since it appears to be the issue:

Move your router.get('/list', ...) outside of the router.post('/', ...).

The way you have it now, the router.get() is not active until after the router.post() is executed and a new router.get() will be registered every time the router.post() is run. Neither of these is correct and will lead to problems.

So, put the router.get() at the same level as the router.post().

Upvotes: 1

Related Questions