【问题标题】:Then is not a function promise error [closed]然后不是函数承诺错误[关闭]
【发布时间】:2017-12-09 07:30:28
【问题描述】:

我是 Promise 的新手,我使用 bluebird 文档从异步代码中获取数据

我尝试的是以下内容:

错误是:

getToken.then 不是函数

如何避免?

这个文件connection.js

return connection.getToken.then(function(connToken){

   console.log(connToken);

}).catch({


})

这是moduleB中getToken的代码

const request = require("request-promise");
const Promise = require("bluebird");
module.exports = {


    getToken: function () {


        return new Promise((resolve, reject) => {
            let options = {
                method: 'POST',
                url: 'https://authentication.arc.com/oauth/token',
                headers: {
                    grant_type: 'client_credentials',
                    authorization: 'Q0MDdmMCFiMTc0fGNvlQVRDWThDNDFsdkhibGNTbz0=',
                    accept: 'application/json;charset=utf-8',
                    'content-type': 'application/x-www-form-urlencoded'
                },
                form: {
                    grant_type: 'client_credentials',
                    token_format: 'opaque&response_type=token'
                }
            };


            request(options)
                .then(function (body) {

                    return body;
                })
                .catch(function (err) {
                    return err;          
                });
        })

    }
}

【问题讨论】:

标签: javascript node.js promise bluebird


【解决方案1】:

您需要实际调用该函数。

connection.js

return connection.getToken() // note the parentheses ()
  .then(function(connToken){
    console.log(connToken);
  })
  .catch(function(error){
    console.error(error);
  });

【讨论】:

  • 我仍然没有得到数据......
  • 你仍然遇到同样的错误吗?
  • 没有,但是控制台日志不打印数据……所以我猜我在 getToken 函数中的承诺有问题,你能看看吗?
  • console.logconsole.error 也放入catch 处理程序中,这样您就可以看到发生了什么。我已经更新了我的答案。
  • @Jenny function getToken() { return request(...); } - 这就是你所需要的。
猜你喜欢
  • 1970-01-01
  • 2022-08-14
  • 2023-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 2022-08-03
  • 1970-01-01
相关资源
最近更新 更多