admin管理员组文章数量:1291383
I make a request successfully with querystring params:
// Works
var Promise = require("bluebird");
var request = Promise.promisifyAll(require("request"));
request.get({
url: '/' + 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: '/' + 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?
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?
Share Improve this question asked Sep 10, 2015 at 17:02 Connor LeechConnor Leech 18.9k32 gold badges110 silver badges160 bronze badges2 Answers
Reset to default 5There are gotchas trying to promisfy the request module, the issue that you're running into is just one of them. I remend 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)
})
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);
});
本文标签: javascriptHow do I promisify a GET request with query stringStack Overflow
版权声明:本文标题:javascript - How do I promisify a GET request with query string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741528530a2383615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论