如何使用请求库和Bluebird发送帖子请求?

时间:2014-08-14 20:11:15

标签: javascript node.js request promise bluebird

所以继承我的设置:

var Promise = require("bluebird");
var request = Promise.promisify(require('request'));

这有效:

request('url')

它返回一个承诺

这不是:

request.post('url', {form: {type: 'artist'}})

给了我这个错误:

TypeError: Object function promisified(_arg0,_arg1) { ... }has no method 'post'
at /Users/beamer30/projects/wevo/controllers/music.js:85:30
at Array.map (native)
at MusicSearch.spotifyRelated (/Users/beamer30/projects/wevo/controllers/music.js:84:22)
at MusicSearch.findRelatedArtists (/Users/beamer30/projects/wevo/controllers/music.js:48:7)
at musicController.search (/Users/beamer30/projects/wevo/controllers/music.js:100:17)
at Layer.handle [as handle_request] (/Users/beamer30/projects/wevo/node_modules/express/lib/router/layer.js:76:5)
at next (/Users/beamer30/projects/wevo/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/beamer30/projects/wevo/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/beamer30/projects/wevo/node_modules/express/lib/router/layer.js:76:5)
at /Users/beamer30/projects/wevo/node_modules/express/lib/router/index.js:227:24

关于如何使这项工作的任何想法? 这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:8)

当您宣传请求时,您只会返回已退回的函数。相反 - 使用promisifyAll来宣传整个对象。由于request并非您都没有收到错误。

var Promise = require("bluebird");

var request = Promise.promisifyAll(require('request')); // this will work