最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I send a post request using the Request library and Bluebird? - Stack Overflow

programmeradmin0浏览0评论

So heres my setup:

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

This works:

request('url')

It returns a promise

This doesn't:

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

gives me this error:

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

any ideas on how to make this work? What is the correct way to do this?

So heres my setup:

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

This works:

request('url')

It returns a promise

This doesn't:

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

gives me this error:

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

any ideas on how to make this work? What is the correct way to do this?

Share Improve this question asked Aug 14, 2014 at 20:11 gbachikgbachik 1,7063 gold badges17 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

When you promisify the request return you promisify only the returned function. Instead - use promisifyAll which promisifies a whole object. Since request is both you didn't get an error.

var Promise = require("bluebird");

var request = Promise.promisifyAll(require('request')); // this will work
发布评论

评论列表(0)

  1. 暂无评论