gpt4 book ai didi

javascript - 如何使用 Request 库和 Bluebird 发送 post 请求?

转载 作者:数据小太阳 更新时间:2023-10-29 06:01:51 27 4
gpt4 key购买 nike

所以这是我的设置:

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

这个有效:

request('url')

它返回一个 promise

这不是:

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

关于如何使这项工作有任何想法吗?执行此操作的正确方法是什么?

最佳答案

当您 promise 请求返回时,您只 promise 返回的函数。相反 - 使用 promisifyAll 来 promise 整个对象。因为 request 是你没有得到错误。

var Promise = require("bluebird");

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

关于javascript - 如何使用 Request 库和 Bluebird 发送 post 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25316777/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com