1

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?

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.