Passing json data using angular services

419
August 21, 2017, at 3:11 PM

I have been trying to post data to node api using angular2 services via json request .However my node api receiving undefined value when I pass parameters thorough angular2 services . Below here is my angular service code

 enrolldegree(name,depart,enrollnumber,cgpa,university,token){
 let peer = '["localhost:10151","localhost:10351"]';
 let fcn = 'initDegree';    
 let argument = '["'+name+'","'+depart+'","'+enrollnumber+'","'+cgpa+'","'+university+']';
 let headers = new Headers({'cache-control':'no-cache', 'Content-Type': 'application/json', 'authorization':'Bearer '+token});
 let options = new RequestOptions({ headers: headers });
 let body1 = new URLSearchParams();
 body1.set('peers','["localhost:10151","localhost:10351"]');
 body1.set('fcn',fcn);
 body1.set('args',argument);
 let body = JSON.stringify(body1);
 console.log('server logs',body);
 return this.http.post('http://localhost:4000/channels/mychannel/chaincodes/mycc', body, options )
.map((res: Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error shit bang in'));
}

Here is my node api code

  app.post('/channels/:channelName/chaincodes/:chaincodeName', function(req, res) {
logger.debug('==================== INVOKE ON CHAINCODE ==================');
var peers = req.body.peers;
var chaincodeName = req.params.chaincodeName;
var channelName = req.params.channelName;
var fcn = req.body.fcn;
var args = req.body.args;
logger.debug('channelName  : ' + channelName);
logger.debug('chaincodeName : ' + chaincodeName);
logger.debug('fcn  : ' + fcn);
logger.debug('args  : ' + args);
if (!peers || peers.length == 0) {
    res.json(getErrorMessage('\'peers\''));
    return;
}
if (!chaincodeName) {
    res.json(getErrorMessage('\'chaincodeName\''));
    return;
}
if (!channelName) {
    res.json(getErrorMessage('\'channelName\''));
    return;
}
if (!fcn) {
    res.json(getErrorMessage('\'fcn\''));
    return;
}
if (!args) {
    res.json(getErrorMessage('\'args\''));
    return;
}
invoke.invokeChaincode(peers, channelName, chaincodeName, fcn, args, req.username, req.orgname)
.then(function(message) {
    res.send(message);
});
 });

When I try to post data using curl query , everything works fine for me . This is curl query which I use in making post request

curl -s -X POST \
  http://localhost:4000/channels/mychannel/chaincodes/mycc \
  -H "authorization: Bearer $ORG1_TOKEN" \
  -H "content-type: application/json" \
  -d '{
    "peers": ["localhost:7051", "localhost:8051"],
    "fcn":"initDegree",
    "args":["Khurrum","software","Ned11831314","3.5","Ned"]
}'

What I am doing wrong in my angular2 services?

Answer 1

In your angular service code, instead of creating your request body as a URLSearchParams instance, try this:

let body1 = {
 peers: ["localhost:10151","localhost:10351"],
 fcn: fcn,
 args: argument
}
let body = JSON.stringify(body1);
Rent Charter Buses Company
READ ALSO
Bytes range with Google Cloud Storage in Express

Bytes range with Google Cloud Storage in Express

I'm using google cloud storage for hosting my mediaI've been trying to get Chrome to properly buffer the audio when serving it through my Express app

219
Express.js + body-parser: empty req.body from POST request

Express.js + body-parser: empty req.body from POST request

I am using Express and body-parser middleware to process incoming requestsOn the front end, I have a form that's just a hidden input and a submit button (written in Pug):

459
How to trigger Express error middleware?

How to trigger Express error middleware?

I'm trying to unit test this piece of code in Mocha:

340