Search code examples
javascriptangularjsnode.jsencodingexpress-4

Returning success using res.send


I am using combination of Node and Angualr JS alongwith express-4

In my code I am trying to send newSourceId via res.send . I am able to get the newSourceId but when I send it using res.send it gets into error instead of getting in to success.

I also tried using res.sendStatus(newSourceId) but no luck!

Following is the Node JS part:

app.post('/addObservationDetail',function(req,res){

var newSourceId = -1;

 Observation.create({obv_source:req.body.source,obv_waveband:req.body.waveband,prpsl_id:req.body.pId}
 ).then(function(result) {

        Observation.findAll({
            attributes: [['obv_id','id']],where:{prpsl_id:req.body.pId},order: [["obv_id","DESC"]],limit: 1
            }) .then(function(rows) {
                console.log("Obv_Id is = "+rows[0].dataValues.id);
                newSourceId=rows[0].dataValues.id;
                console.log("newsrc===="+newSourceId);
                 res.send(newSourceId);
            }); 
 });
});

This is the Angular JS part:

$scope.updateObsDet = function(obs_detail,index) {
            var url = "/";
            if(obs_detail.sourceId == null){
                url += "addObservationDetail";
                $http({
                    method:'POST',
                    url:url,
                    headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest:function(obj){
                        var str = [];
                        for(var p in obj)
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        return str.join("&");
                    },
                    data:{source:obs_detail.source,waveband:obs_detail.waveband,pId:$scope.proposal.proposalId}                     
                }).success(function(data){
                    alert("New observation added");       //should come in here
                    updateSourceId(data,index,obs_detail);
                }).error(function(data){
                    alert(data);         //getting in here
                });

            } else {
                url += "updateObservationDetail";
                data = {
                        source:obs_detail.source,
                        waveband:obs_detail.waveband,
                        sourceId:obs_detail.sourceId
                        };
                $http.post(url,data).success(function(data){
                    alert(data);
                }).error(function(data){
                    alert(data);
                });
            }
        };

PS: I can not change the angular Part of the code, changes need to be done in the Node JS part.


Solution

  • yes you can set json response try this

    var http = require('http');
    
    var app = http.createServer(function(req,res){
        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify({ a: 1 }));
    });
    app.listen(3000);