0

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.

Abhishek
  • 878
  • 12
  • 28
  • 1
    did you try to send as json. may be this will help you ?? and **Observation.findAll** this will always return some records?? – Muhammad Usman Sep 14 '15 at 06:05
  • @MuhammadUsman yes, it will always be returning records, how to set the type as JSON, do we need to mention it separately, I am using `express-4` – Abhishek Sep 14 '15 at 06:08
  • please see this answer to return json response http://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express – Muhammad Usman Sep 14 '15 at 06:15
  • 1
    use res.json({key:value}) – Subham Sep 14 '15 at 06:22
  • @MuhammadUsman sending it as JSON helped!! but when when we do `res.send ` isn't it supposed to be sending it as JSON by default.? – Abhishek Sep 14 '15 at 06:26

1 Answers1

1

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);
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33