【问题标题】:Javascript promise not working how I expectJavascript 承诺不能按我的预期工作
【发布时间】:2019-02-05 10:26:12
【问题描述】:

我正在尝试处理承诺,但我似乎无法正常工作。 我正在将 NodeJS 与 Express 一起使用

我的代码的预期结果:Console.log 应该打印响应正文

实际发生了什么:console.log 打印出undefined

http = require('http'),
req = require('request'),
reqprom =  require('request-promise'),


reqprom('myuri')
.then(function(request, response) {
    console.log(response.body);

})
.catch(function(err) {
    console.log(err);
});
				  
				  

【问题讨论】:

  • 也许 response.body 未定义。 reqprom 是什么?
  • 抱歉我错过了一个要求 reqprom = require('request-promise'),
  • 这取决于你的 myuri,它返回的响应是什么
  • 看起来不应该有 两个 值传递给then...!? npmjs.com/package/request-promise
  • 不应该response是then的第一个参数,而不是第二个?

标签: javascript node.js express promise


【解决方案1】:

request-promise 的示例所示,response 应该是then 的第一个(也是唯一一个)参数,因此您的代码应如下所示:

reqprom('myuri')
.then(function(response) {
    console.log(response.body);    
})
.catch(function(err) {
    console.log(err);
});

【讨论】:

  • 非常感谢,我不敢相信我错过了这个!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2019-12-03
  • 2019-01-19
  • 2017-10-26
  • 2018-04-09
相关资源
最近更新 更多