Reputation: 18834
I make a request successfully with querystring params:
// Works
var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"));
request.get({
url: 'https://api.angel.co/1/tags/' + encodeURIComponent(friscoLocationTag) + '/startups',
qs: {
access_token: myToken,
order: 'popularity'
},
method: 'GET'
}, function(error, response, body){
// request success
console.log(body);
});
However when I try to promisfy my request I am failing:
// Does Not Work
var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"));
request.get({
url: 'https://api.angel.co/1/tags/' + encodeURIComponent(friscoLocationTag) + '/startups',
qs: {
access_token: myToken,
order: 'popularity'
},
method: 'GET'
}).then(function(error, response, body){
console.log(body);
});
This gives the error:
}).then(function(error, response, body){
^
TypeError: undefined is not a function
at Object.<anonymous> (/Users/connorleech/Projects/startup-locator/server/routes.js:36:4)
How do I properly promisify my get request using bluebird?
Upvotes: 2
Views: 10391
Reputation: 73211
The promisified function is called getAsync
. See docs
var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"));
request.getAsync({
url: 'https://api.angel.co/1/tags/' + encodeURIComponent(friscoLocationTag) + '/startups',
qs: {
access_token: myToken,
order: 'popularity'
},
method: 'GET'
}).then(function(error, response, body){
console.log(body);
});
Upvotes: 4
Reputation: 11677
There are gotchas trying to promisfy the request module, the issue that you're running into is just one of them. I recommend you to instead look at request-promise which is basically what you're trying to do, it is an implementation of the request module using bluebird promises
EDIT:
Install request-promise
npm install request-promise --save
Then:
var request = require('request-promise');
request({
url: 'https://api.angel.co/1/tags/' + encodeURIComponent(friscoLocationTag) + '/startups',
qs: {
access_token: myToken,
order: 'popularity'
},
method: 'GET'
})
.then(function(body){
console.log(body)
})
Upvotes: 5