Gardezi
Gardezi

Reputation: 2842

success function is not working angular

I am sending http request and when that request is finished I am trying to go to another state but the problem is it does not goes in the success callback I thought my be I'm getting an error so I wrote the error callback it does not goes in that to. Can anybody tell me what am I doing wrong

  $scope.submitUpdatedData= function(user){
    debugger;
    // $http.post('/url',{params: value}).sucess(function(){
    API.updateRecord(user).success(function(res){
      $state.go('app' ,{} , {reload: true });
      console.log("Hello");
    });
  }

The API code is given below. Here I invoke the http call

.factory('API', function($http) {
  var api = {};
  var baseURL = 'http://localhost:3000';

  api.addRecord = function(record) {
      console.log(record);
    // $http.post(baseURL + '/addData', {name: record.name}.);
      return $http.post(baseURL + '/addData', {rec:record});
  };

  api.deleteRecord = function(id){
    return $http.get(baseURL +'/delete/' + id );
  };

  api.updateRecord  = function(user){
    return $http.post(baseURL + "/update/" ,{rec:user});
  };

  api.getAllRecord = function(){
    return $http.get(baseURL+'/getAll');
  };

  api.getOneRecord = function(id){
    return $http.get(baseURL + '/getOne/' + id)
  };

  return api;
})

UPDATE

I have replaced the .success part with then but it still not works

Second Update

This is my server side code

  var express = require('express');
var mongoose = require('mongoose');
var util = require('util');
var bodyParser = require('body-parser')
var app = express();
var Schema = mongoose.Schema;
require('node-monkey').start({host: "127.0.0.1", port:"50500"});

var allowCrossDomain = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header('Access-Control-Allow-Headers', 'Content-Type');

  next();
};
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
}));
// app.use(express.json());       // to support JSON-encoded bodies
// app.use(express.urlencoded()); // to support URL-encoded bodies
app.use(allowCrossDomain);
// app.use('/' , require('./index'))
mongoose.connect('mongodb://localhost:27017/myappdatabase');

var userSchema = new Schema({
  name:  String,
  password: String
});



var Todo = mongoose.model('Todo', userSchema);


app.get('/getAll' , function(req, res){
    Todo.find({} , function(err , todos){
      if (err){
        res.send(err);
      }
      console.log(todos);
      res.send(todos);
    });
});

app.get('/delete/:name' , function(req , res){
  console.log(req.params);
  console.log(req.params.name);
  Todo.remove({
            name : req.params.name
        }, function(err, todo) {
            if (err)
                res.send(err);
            // get and return all the todos after you create another
            Todo.find(function(err, todos) {
                if (err)
                    res.send(err)
                res.json(todos);
            });
        });
});

app.get('/getOne/:id' , function(req , res){
  Todo.find({name : req.params.id}, function(err, todo) {
            if (err)
                res.send(err);
            res.send  (todo[0]);
            // get and return all the todos after you create another
        });
});


app.post('/update', function(req , res){
  console.log(req.param('rec').name);
  Todo.update({_id:req.param('rec').id} , {$set : {name:req.param('rec').name , password:req.param('rec').password}} , function(err){
    if(err)
      res.send("Error occured");
    res.send("true");
  });
});


app.post('/addData' , function(req , res){
  console.log( req.param('rec').name);

  var p = new Todo({name: req.param('rec').name  , password: req.param('rec').password});
  p.save(function(err){
    if(err){
      res.send(err);
      console.log(error);
    }
    res.json(p);
  });
});


var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});



// module.exports = app;

Upvotes: 3

Views: 577

Answers (2)

Paul Georg Podlech
Paul Georg Podlech

Reputation: 530

Seems like your request is never answered by the API Server. Maybe you can set a timeout for your request. Here it says you can do:

$http.post(url, data, {timeout: 100});

That should timeout your request after 100ms.

Upvotes: 0

taxicala
taxicala

Reputation: 21759

Seems like success and error are deprecated, you should use then instead:

API.updateRecord(user).then(function(res){
    $state.go('app' ,{} , {reload: true });
    console.log("Hello");
});

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error. Source here

Upvotes: 3

Related Questions