NodeJS Request return JSON from function - javascript

I've read a couple of posts about this here (callbacks) but I still don't really fully understand how to solve my problem. So I was hoping that somebody here could help me with mine and I would get it better.
Simple put I want the ID I get from the first request to be used for the second request.
I'm new to JavaScript and NodeJS in general.
function idRequest(name) {
var options = {
...
};
function callback(error, response, body) {
if (response.statusCode == 200 && !error) {
const info = JSON.parse(body);
//console.log(info.accountId);
return info.accountId;
}
}
request(options, callback);
}
function requestById(accountId) {
var options = {
...
};
function callback(error, response, body) {
if (response.statusCode == 200 && !error) {
const info = JSON.parse(body);
console.log(info);
}
}
request(options, callback);
}
var id = idRequest('..');
requestById(id);

Try by returning a promise from the first function and inside it resolve the callback, so the once it is resolved , you can use it's then to trigger the second function
function idRequest(name) {
var options = {
...
};
function callback(error, response, body) {
if (response.statusCode == 200 && !error) {
const info = JSON.parse(body);
//console.log(info.accountId);
return info.accountId;
}
}
return new Promise(function(resolve, reject) {
resolve(request(options, callback))
})
}
function requestById(accountId) {
var options = {
...
};
function callback(error, response, body) {
if (response.statusCode == 200 && !error) {
const info = JSON.parse(body);
console.log(info);
}
}
request(options, callback);
}
var id = idRequest('..').then(function(data) {
requestById(data);
});

since callback is a async call, so var id will be undefined, when you call the requestById(id);
so either you can use the promise method, answered by #brk or you can call your requestById(id) function directly from the first callback.

Related

JavaScript Promise - how to make multiple promise?

How can I chain the multiple promise? For instance:
var promise = new Promise(function(resolve, reject) {
// Compose the pull url.
var pullUrl = 'xxx';
// Use request library.
request(pullUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Resolve the result.
resolve(true);
} else {
reject(Error(false));
}
});
});
promise.then(function(result) {
// Stop here if it is false.
if (result !== false) {
// Compose the pull url.
var pullUrl = 'xxx';
// Use request library.
request(pullUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
resolve(body); // <-- I want to pass the result to the next promise.
} else {
reject(Error(false));
}
});
}
}, function(err) {
// handle error
});
promise.then(function(result) {
// Stop here if it is false.
if (result !== false) {
// handle success.
console.log(result);
}
}, function(err) {
// handle error.
});
Error:
resolve(body);
ReferenceError: resolve is not defined
Any ideas?
When chaining Promises, then return value from a function given to then should either be a Promise, or the value to pass along.
In your case, since you're making an async call, you'll just return another promise and call reject or resolve within there. If it wasn't async, you could just return the value, or throw an error, which also gets passed along to the next then or error handler/catch as appropriate.
Also, you need to chain them together, because each then() returns a different Promise.
So, something like this:
var promise = new Promise(function(resolve, reject) {
// Compose the pull url.
var pullUrl = 'xxx';
// Use request library.
request(pullUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
// Resolve the result.
resolve(true);
} else {
reject(Error(false));
}
});
});
promise.then(function(result) {
// Stop here if it is false.
if (result !== false) {
var airportCode = result;
// Compose the pull url.
var pullUrl = 'xxx';
// Use request library.
return new Promise(function (resolve, reject) {
request(pullUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
resolve(body);
} else {
reject(Error(false));
}
});
});
}
}).then(function(result) {
// Stop here if it is false.
if (result !== false) {
// handle success.
console.log(result);
}
}).catch(function (err) {
// handle error
});
Here is a JSFiddle with a working version: JSFiddle

Node Async - Working async method returns undefined inside async.parallel

Basically I have a method fetching values from an API and works, but inside async.parallel the results listed are all undefined.
Here's the method:
function getNumberOfSharesFromFacebookApi(url ,callback) {
request(facebookApiUrl + url + '&format=json', function (error, response, body) {
let res = 0;
if (!error && response.statusCode == 200) {
try {
res = JSON.parse(body)[0]['total_count'];
} catch(err) { }
}
callback(res);
});
}
Here's the async call:
async.parallel ([
callback => {
getNumberOfSharesFromFacebookApi(urlsToTest[0], callback);
},
callback => {
getNumberOfSharesFromFacebookApi(urlsToTest[1], callback);
},
callback => {
getNumberOfSharesFromFacebookApi(urlsToTest[2], callback);
},
callback => {
getNumberOfSharesFromFacebookApi(urlsToTest[3], callback);
}
],
(err, results) => {
console.log(results);
});
on your request function, place your response as the 2nd argument of the callback. request uses node style callbacks wherein the 1st argument is an error.
function getNumberOfSharesFromFacebookApi(url ,callback) {
request(facebookApiUrl + url + '&format=json', function (error, response, body) {
if(error) return callback(error); //callback on error
let res = 0;
if (!error && response.statusCode == 200) {
try {
res = JSON.parse(body)[0]['total_count'];
} catch(err) { }
}
callback(null, res);
});
}

Chaining multiple request using bluebird

I'm trying to convert my existing code using BlueBird, please suggest a best option to chain multiple request. Error happening in each callback needs to be redirected to rendered with different error.
request(option1, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data= JSON.parse(body);
if(data.valid){
if(data.expired){
next();
} else {
request(option2, function (error2, response2, body2) {
var data2= JSON.parse(body2);
if(data2.valid) {
request(option3, function (error3, response3, body3) {
next();
})
} else {
res.json({error:'Error1'});
}
})
}
} else {
res.json({error:'Error2'});
}
} else {
res.json({error:'Error3'});
}
})
This is pretty straightforward, also note your current code doesn't handle errors in the second and third requests and this does:
var request = require("request-promise"); // request - converted to bluebird
request(option1).then(data=> {
if(!data.valid) throw Error("Error3");
if(data.expired) return;
return request(option2).then(JSON.parse);
}).then(data2 => {
if(!data2) return; // didn't need to fetch additional data
if(!data2.valid) throw Error("Error2");
return request(option3);
}).then(() => {
next();
}, e => {
res.json(error: e.message);
// better log this.
});
var rp = require('request-promise');
function handleError(err) {
res.json({
error: err.message
});
}
function parse(data) {
if (data) {
return JSON.parse(data);
}
}
rp(option1)
.then(parse)
.then(function (data) {
if (!data || !data.valid) {
throw Error('Error2');
}
if (data.expired) {
return;
}
return option2;
})
.then(rp)
.then(parse)
.then(function (data2) {
if (!data2 || !data2.valid) {
throw Error('Error1');
}
return option3;
})
.then(rp)
.then(parse)
.then(function () {
next();
})
.catch(handleError);
You don't need to manually check for statusCode but if you need to do so, first you have to add resolveWithFullResponse attribute to your option1 object, which allows you to receive the response object:
function checkStatusCode(response) {
if (response.statusCode !== 200) {
throw Error('Error3');
}
return response.body;
}
// add resolveWithFullResponse attribute to option1
option1.resolveWithFullResponse = true;
rp(option1)
.then(checkStatusCode)
.then(parse)
//...

NODE JS cancel request

I am using this code to download files in node js :
var currentVideoRequest = null;
window.spawnVideoPlayer = function (url, subs, movieModel,tag) {
if(currentVideoRequest) {
try
{
currentVideoRequest.abort();
}
catch(err)
{
alert(err.message);
}
}
var fs = require('fs');
var urlrequest = currentVideoRequest = require('request');
urlrequest.get({url: url, encoding: 'binary'}, function (err, response, body) {
fs.writeFile(FILEURL, body, 'binary', function(err) {
});
});
}
And in the currentVideoRequest.abort(); i get this error:
Object function request(uri, options, callback) {
if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')
if ((typeof options === 'function') && !callback) callback = options
if (options && typeof options === 'object') {
options.uri = uri
} else if (typeof uri === 'string') {
options = {uri:uri}
} else {
options = uri
}
options = copy(options)
if (callback) options.callback = callback
var r = new Request(options)
return r
} has no method 'abort'
To add to #Etai's answer, you need to require the request module before using it for one instance of the request. Something like this:
var request = require('request');
// ...
// then later in the code
var urlrequest = request.get(uri, function(err, response, body) {
// process data here
});
// later, you'd abort this as:
urlrequest.abort();
Note that I'm saving the instance with var urlrequest = request.get(params, callback); so that I can call abort on it later.
your currentVideoRequest is a constructor for a request object, not a request object, which is why this is failing.
The request constructor returns a request object when invoked, i.e.
require('request')('uri', function(err, resp, body){})
you can use abort() method to stop that request.
var reqObj = request({uri: 'https://jsonplaceholder.typicode.com/albums' }, function (error, response, body) {
console.log('API requested ') ;
if (!err){
console.log(body);
}
else
{
console.log(err);
}
});
reqObj.abort();
I think you can use this method to get what you need.
I used async in this way, which makes both the code cleaner and less callback .
(async function main() {
try {
let urlRequest = await customRequest("http://127.0.0.1:8000/api/product/search?title=foo");
urlRequest.abort();
} catch (e) {
console.log(e);
}
})();
function customRequest(url) {
return new Promise((resolve, reject) => {
request.get(url, (err, res) => {
if (err) reject(err)
if (res.statusCode !== 200)
reject("my Error ");
resolve(res);
})
})
}
Also, if you do not need to answer the urlRequest variable,you can remove the await from the function as shown below.
try {
let urlRequest = customRequest("http://127.0.0.1:8000/api/product/search?title="); // run in background !
urlRequest.abort();
} catch (e) {
console.log(e);
}
Finally, since our request returns an exception if you make a mistake, you can write abort() in the catch block if needed.
(async function main() {
try {
var urlRequest =await customRequest("http://127.0.0.1:8000/api/product/search?title=");
} catch (e) {
urlRequest.abort();
console.log(e);
}
})();
Also, because the customRequest() function returns a promise note, you use then() instead of async await.

How to recurse asynchronously over API callbacks in node.js?

An API call returns the next 'page' of results. How do I recurse over that result callback elegantly?
Here is an example of where I need to do this:
var url = 'https://graph.facebook.com/me/?fields=posts&since=' + moment(postFromDate).format('YYYY-MM-DD') + '&access_token=' + User.accessToken;
request.get({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
_.each(body.posts.data, function (post) {
User.posts.push(post); //push some result
});
if (body.pagination.next) { // if set, this is the next URL to query
//?????????
}
} else {
console.log(error);
throw error;
}
});
I would suggest wrapping the call in a function and just keep calling it until necessary.
I would also add a callback to know when the process has finished.
function getFacebookData(url, callback) {
request.get({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
_.each(body.posts.data, function (post) {
User.posts.push(post); //push some result
});
if (body.pagination.next) { // if set, this is the next URL to query
getFacebookData(body.pagination.next, callback);
} else {
callback(); //Call when we are finished
}
} else {
console.log(error);
throw error;
}
});
}
var url = 'https://graph.facebook.com/me/?fields=posts&since=' +
moment(postFromDate).format('YYYY-MM-DD') + '&access_token=' + User.accessToken;
getFacebookData(url, function () {
console.log('We are done');
});

Categories

Resources