For example if I like to do:
var request = require('request');
function callback(error, response, body) {
if (num) {
console.log(num);
}
console.log(body);
}
var cookie = {
'74358942795': 'abd7fce573c2-18c2c0de-037f-4aef-9235',
'58942743795': 'e3101e9a0e28-b596998e-e879-4003-a724'
}
function options(num) {
return {
url: 'http://.../.../',
method: 'POST',
headers: {
...
'Cookie': cookie[num]
},
body: '...=' + Date.now()
};
}
(function(num){
request(options(num), callback);
})('74358942795');
Here within the callback it will return num undefined and throw an error.
Igor's answer is almost right. It should be:
function callback(error, response, body) {
var that = this;
if (that.num) {
console.log(that.num);
}
console.log(body);
}
(function(num){
request(options(num), callback.bind({num}));
})('74358942795');
num isn't available to the callback scope. Simplest solution I see would be to replace that last block with:
(function(num){
request(options(num), function (error, response, body){
if (num) {
console.log(num);
}
console.log(body);
});
})('74358942795');
I don't get how "forloop" is related to the code in question. Use bind:
function callback(num, error, response, body) {
if (num) {
console.log(num);
}
console.log(body);
}
(function(num){
request(options(num), callback.bind(null, num));
})('74358942795');
function callback(num, error, response, body) {
if (num) {
console.log(num);
}
}
(function(num) {
setTimeout(callback.bind(null, num), 1000);
})('74358942795');
Related
I have a scenario where i need to take response (body) of request method outside request. How can i do it?
request.post({
url: 'http://localhost/api/messages',
form: { key: message }
}, function (err, httpResponse, body) {
tmsg = body;
})
console.log(tmsg);
I need this tmsg outside for next processing, Actual scenario is as below.
app.post('/incomemsg', function (req, res) {
var mediaCount = req.body.NumMedia;
if (mediaCount == 0) {
//var twiml = getResponse(message);
var twiml = new MessagingResponse();
request.post({
url: 'http://localhost:3978/api/messages',
form: { key: message }
}, function (err, httpResponse, body) {
tmsg = body;
})
console.log(tmsg);
}else {
//dosomething which outputs tmsg
}
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(tmsg.toString());
});
The problem is you are trying to assign value to a global variable in request.post's callback() which is only called after request.post is executed by Asynchronous logic(API calls are all async), so a better way would be to promisify request.post and await the request.post to make it seem synchronous.
const requestPromisified = requestObject =>
new Promise((resolve, reject) => {
request.post(requestObject, function(err, httpResponse, body) {
if (err) {
reject(err);
}
resolve(body);
});
});
const body = await requestPromisified({
method: "POST",
url: "http://localhost/api/messages",
form: { key: message }
});
You only can do something with tmsg when you made the request so you need to rearrange your code like this:
app.post('/incomemsg', function (req, res) {
var mediaCount = req.body.NumMedia;
var twiml = new MessagingResponse();
request.post({
url: 'http://localhost:3978/api/messages',
form: { key: message }
}, function (err, httpResponse, body) {
tmsg = body;
console.log(tmsg);
if (mediaCount === 0) {
//do something with tmsg
} else {
//do something else with tmsg
}
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(tmsg.toString());
});
});
Otherwise tmsg will be null because there was no request made to fill that variable.
var request = require('request');
var boardsCall = {
method: 'GET',
url: 'https://api.trello.com/1/organizations/xxxxxxxxxx/boards?filter=open&fields=id,name',
qs: {
key: 'xxxxxxxxxxxxxxxx',
token: 'xxxxxxxxxxxxxxxxxxxxxxxxx'
}
};
function test(url, callback) {
request(url, function(error, response, body) {
if (error) {
return callback(error);
}
callback(null, JSON.parse(body));
})
}
const x = test(boardsCall, function(err, body) {
if (err) {
console.log(err);
}
else {
return body;
}
})
console.log(x);
how can I use the value of the body outside?
to use it in other methods later
I am open to any changes best practices, I read a lot and got a bit confused on the topic of callbacks, promises async await.
In my approach request is wrapped with Promise, test function returns Promise response. Inside the main method test function will be executed synchronously. Once the response value assigned to x do remaining processing logic inside the main() method.
var request = require('request');
var boardsCall = {
method: 'GET',
url: 'https://api.trello.com/1/organizations/xxxxxxxxxx/boards?filter=open&fields=id,name',
qs: {
key: 'xxxxxxxxxxxxxxxx',
token: 'xxxxxxxxxxxxxxxxxxxxxxxxx'
}
};
function test(url) {
//Wrapping request callback with Promise
return new Promise((res, rej)=> {
request(url, function(error, response, body) {
if (error) {
rej(error);
}
res(JSON.parse(body));
})
})
}
async function main() {
try {
const x = await test(boardsCall);
console.log("Result : ", x );
// Remaining logic to process based on x value
} catch(e) {
console.error("Error :", e);
}
}
//Calling main method
main()
I have set up a server-application with NodeJS/Express and am trying to get the following script to work so that it stores the return value of a http-request in a variable for later use.
function checkIfVerified(req, res) {
var user_id = req.user.id
var options = {
method: 'GET',
url: 'https://MYDOMAIN/api/v2/users/' + user_id + '?fields=email_verified&include_fields=true',
headers:
{
authorization: 'Bearer ' + app.locals.token
},
body:
{
},
json: true
}
function check(callback) {
request(options, function (error, response, body) {
return callback(null, body)
})
}
var email_verified = check(function (err, data) {
if (!err) {
return data
}
})
console.log(email_verified)
return email_verified
}
For some reason the variable 'email_verified' doesn't hold any value ...
Thanks for your help!
Cheers
Philip
So after #Derlins clue I found out the following solution to the question:
function check() {
return new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if (error) {
reject(error)
} else {
resolve(body.email_verified)
}
})
})
}
res.locals.email_verified = await check()
This does the trick :-)
Probably an obvious answer to this but I'm not sure what way to take.
request is a node module: https://github.com/request/request
I fill an array of getHistory requests (with different parameters). p = [p1,p2...].
this.app.all('/api/heatmap', function(req,res) {
// fill p here _.each(blabla, p.push(gethistory(someparams...)
var result = [];
function getHistory(params) {
var options = { ...};
var callback = function(error, response, body) {
if(error) { //wtv
} else {
// what to do with the body here ? return body ? result.push(body) ?
}
}
request(options, callback);
}
Q.all(p).then(function() {
});
}
So the problem here is that I when all of the request to be done , put everything in an array/object then send the whole thing to the client. How to have getHistory returning the fetched value (after the request is done ).
Hope it's clear.
The core problem here is that node.js-style callbacks and promises are not compatible. Promises emphasize on return values, node emphasizes on callbacks.
Therefore you need a sort of adapter that wraps node's callback convention properly, a process called Promisifcation. This can be done manually, but it's tedious at best and error-prone when you are not careful. Luckily, since node's conventions are well-established, it can be automated. Q has a few helpers for that, but Bluebird is quite a bit more convenient in this regard.
So the easy way to do it is to switch to Bluebird as the promise library and to use promisifyAll.
var Promise = require('bluebird');
var request = Promise.promisifyAll(require("request"));
this.app.all('/api/heatmap', function(req, res) {
var requests = blabla.map(function (item) {
return request.getAsync({ /* params */ });
});
Promise.all(requests).then(function (responses) {
res.send( JSON.stringify(responses) ); // whatever
}).catch(function (error) {
res.send( "An error ocurred: " + error ); // handle error
});
}
FWIW, here's another answer that shows how the same would look like when done properly with Q:
// promisified request
function requestAsync(options) {
var result = Q.defer();
request(options, function(error, response, body) {
if (error) {
result.reject(error);
} else {
result.resolve(body);
}
});
return result.promise;
}
// returns promises for heatmapVolumes
function getHistory(params) {
return requestAsync({
method: 'GET',
url: 'https://api.kaiko.com/v1/charts/' +
encodeURIComponent(params.exchange) + '/' +
encodeURIComponent(params.pair) +'/history',
qs: params.qs,
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}).then(function (body) {
return heatmapVolume(body, params.exchange, params.pair);
}).catch(function (error) {
// log detailed error and send less revealing message downstream
console.error('error fetching trades', error);
throw new Error('Something went wrong while fetching trades');
});
}
// usage
this.app.all('/api/heatmap', function(req, res) {
getHistory({
exchange: "foo", pair: "bar", qs: "qux"
}).then(function (heatmap) {
res.send(200, heatmap);
}).catch(function (error) {
res.send(500, error);
});
});
Used Q.deferred and it worked as documented \o/
function getHistory(params) {
var deferred = Q.defer();
var options = {
method: 'GET',
url: 'https://api.kaiko.com/v1/charts/' + params.exchange + '/' + params.pair +'/history',
qs:qs,
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}
var callback = function(error, response, body) {
if(error) {
console.log('error fetching trades', error);
res.send(500, 'Something went wrong while fetching trades');
} else {
var body = heatmapVolume(body, params.exchange, params.pair);
// console.log("result!", body.exchange, body.pair);
result.push(body);
// return body;
deferred.resolve();
}
}
request(options, callback);
return deferred.promise;
}
I'm trying a pretty complex computation in my code below. I'm trying to get the list of bugs from github in the given project using the api https://api.github.com/repos/marklogic/java-client-api/issues?page=1&per_page=10. From the list of bugs I'm trying to get each issues' corresponding events and comments from their corresponding endpoints ex: https://api.github.com/repos/marklogic/java-client-api/issues/291/events and https://api.github.com/repos/marklogic/java-client-api/issues/291/comments.
I'm using async library. I'm using waterfall function and parallel function to return a consolidated JSON for each bug such that each issue will have comment, & events in the same response for each issue. The problem is its throwing Can't set headers after they are sent error & its pointing to line 2 lines, I understand what the error is saying but I can't figure out how to fix it, because commenting out either of the offending lines results in request hang because the server is not sending the response. Please help! Thanks in advance
exports.listGitHubBugs = function(req, res) {
var _page = req.query.page || 1;
var _per_page = req.query.per_page || 25;
var finalResult = []
//console.log('url:', 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page);
var options = {
url: 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page,
headers: {
'User-Agent': req.query.project
},
auth: githubAuth
};
request(options, function(error, response, body) {
if (error) {
console.log(error);
res.send(error);
}
if (!error && response.statusCode === 200) {
var issues = JSON.parse(response.body)
async.waterfall([
// get comments & events for all bugs and then send the response
function(callback) {
issues.forEach(function(issue) {
// for each bug, get comments and events
async.parallel([
function(parallelCallback) {
var options = {
url: issue.events_url,
headers: {
'User-Agent': getProjectNameFromURL(issue.events_url)
},
auth: githubAuth
};
request(options, function(error, response, body) {
if (error) {
console.log('ERROR', error);
parallelCallback(error)
}
if (!error && response.statusCode === 200) {
// console.log('events:', body);
parallelCallback(null, body)
}
})
},
function(parallelCallback) {
var options = {
url: issue.comments_url,
headers: {
'User-Agent': getProjectNameFromURL(issue.comments_url)
},
auth: githubAuth
};
request(options, function(error, response, body) {
if (error) {
console.log('ERROR', error);
parallelCallback(error)
}
if (!error && response.statusCode === 200) {
// console.log('comments:', body);
parallelCallback(null, body)
}
})
}
], function(err, result) {
if (err) {
console.log('ERROR:', err);
callback(err);
}
console.log('parallel process done');
issue.events = JSON.parse(result[0]);
issue.comments = JSON.parse(result[1]);
finalResult.push(issue)
callback(null, finalResult) // offending line#1
})
}) // forEach end
}
], function(err, result) {
if (err) {
res.send(err);
}
console.log('waterfall done');
console.log(result);
res.send(result); // offending line#2
})
} // if end
}) // reqest end
}
Error
UncaughtException: Can't set headers after they are sent.
ERROR Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
at ServerResponse.res.set.res.header (/Users/sreddy/space/angularjs/BugTrack/node_modules/express/lib/response.js:524:10)
at ServerResponse.res.send (/Users/sreddy/space/angularjs/BugTrack/node_modules/express/lib/response.js:125:10)
at ServerResponse.res.json (/Users/sreddy/space/angularjs/BugTrack/node_modules/express/lib/response.js:191:15)
at /Users/sreddy/space/angularjs/BugTrack/server/api/common/common.controller.js:163:33
at /Users/sreddy/space/angularjs/BugTrack/server/api/common/common.controller.js:153:29
at /Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:254:17
at done (/Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:135:19)
at /Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:32:16
at /Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:251:21
Final working code
exports.listGitHubBugs = function(req, res) {
var _page = req.query.page || 1;
var _per_page = req.query.per_page || 25;
var finalResult = []
//console.log('url:', 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page);
var options = {
url: 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page,
headers: {
'User-Agent': req.query.project
},
auth: githubAuth
};
request(options, function(error, response, body) {
if (error) {
console.log(error);
return res.send(error);
}
if (!error && response.statusCode === 200) {
var issues = JSON.parse(response.body)
async.waterfall([
// get events and comments for all bugs and return the final processes list of bugs
function getEventsAndCommentsForAllBugs(callback) {
issues.forEach(function getEventsAndComments(issue, index) {
// for each bug, get comments and events
async.parallel([
function getEvents(parallelCallback) {
var options = {
url: issue.events_url,
headers: {
'User-Agent': getProjectNameFromURL(issue.events_url)
},
auth: githubAuth
};
request(options, function(error, response, body) {
if (error) {
console.log('ERROR', error);
parallelCallback(error)
}
if (response.statusCode === 200) {
// console.log('events:', body);
parallelCallback(null, body)
}
})
},
function getComments(parallelCallback) {
var options = {
url: issue.comments_url,
headers: {
'User-Agent': getProjectNameFromURL(issue.comments_url)
},
auth: githubAuth
};
request(options, function(error, response, body) {
if (error) {
console.log('ERROR', error);
parallelCallback(error)
}
if (response.statusCode === 200) {
// console.log('comments:', body);
parallelCallback(null, body)
}
})
}
], function attachEventsAndComments(err, result) {
if (err) {
console.log('ERROR:', err);
callback(err);
}
console.log('parallel process done');
issue.eventList = JSON.parse(result[0]);
issue.commentList= JSON.parse(result[1]);
finalResult.push(issue)
if (index === (issues.length - 1)) {
callback(null, finalResult)
}
//
})
}) // forEach end
}
], function processedBugs(err, result) {
if (err) {
res.send(err);
}
console.log('waterfall done');
console.log(result);
res.send(result);
})
} // if end
}) // reqest end
}
could you provide a complete working example of the code, something we can try.
this said, there are several errors in this source code.
Onthe first request, if an error occurs, you write it in app.response,, but you don t stop execution. Thus, if an error occurs, you ll write twice the response object.
You should do
if (error) {
console.log(error);
return res.send(error);
}
instead of
if (error) {
console.log(error);
res.send(error);
}
Then, this can be changed
if (!error && response.statusCode === 200) {
to
if (response.statusCode === 200) {
Same mistake occurs while fetching issues events and comments, please consider to fix it.
And also in the final callback of async.//
And in the final callback of async.waterfall.
finally, i suggest you to make use of named functions. That would help you to debug by providing more meaningfull error stack trace.
For example instead of doing,
async.prallel([function(){/* code here*/}]);
You would write
async.parallel([function nameOfTheTask(){/* code here*/}]);
consider also to use a linter such as eslint, several missing ; could break your code, see http://eslint.org/
I figured it out. I was calling the parellelCallback() for every iteration instead of calling it at the end of the loop. Simple if condition was all that was required.
if (index === (issues.length-1)) {
callback(null, finalResult)
}