如何使用查询string提交GET请求?

我使用querystring参数成功请求了一个请求:

// 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); }); 

但是,当我试图提出我的请求我失败:

 // 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); }); 

这给出了错误:

 }).then(function(error, response, body){ ^ TypeError: undefined is not a function at Object.<anonymous> (/Users/connorleech/Projects/startup-locator/server/routes.js:36:4) 

我如何使用蓝鸟妥善保证我的请求?

promisified函数被称为getAsync 。 看文档

 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); }); 

有一些问题试图提出请求模块,你遇到的问题只是其中之一。 我build议你去看看request-promise ,它基本上就是你要做的,它是一个使用蓝鸟许诺的请求模块的实现

编辑:

安装请求 – 承诺

 npm install request-promise --save 

然后:

 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) })