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?
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);
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
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
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):