代码之家  ›  专栏  ›  技术社区  ›  Jonas Raphael Schultheiss

承诺链接,使用另一个请求的结果

  •  0
  • Jonas Raphael Schultheiss  · 技术社区  · 6 年前

    我在node.js中使用ES6。长话短说,我现在只想回电话,并希望以承诺取代他们。

    我做了一个测试项目,从api/端点获取oauth2令牌,刷新它,最后撤销它。目标是,将上一个请求的响应提供给下一个请求。我的代码如下所示:

    const oauth2Adapter = require('./api/adapter/oauth2Adapter')
    
    function test () {
    oauth2Adapter.RequestNewAccessToken()
    .then(function (response) {
      console.log(response)
      return oauth2Adapter.RefreshAccessToken(response.body)
    })
    .then(function (response) {
      return oauth2Adapter.RevokeAccessToken(response.body)
    })
    .then(console.log)
    .catch(console.log)
    }
    
    test()
    

    第一个承诺会回报它的回应。下一步是将它作为参数赋给第二个promise。但第二个承诺只接受一个未定义的对象。

    编辑:添加“return”关键字并没有改变这种情况。问题是“RefreshAccessToken”“undefined”接收到。我也不知道这是否有帮助,但下面是'oauth2Adapter.js'代码:

    const Promise = require('promise')
    const rp = require('request-promise')
    const credentials = require('../../misc/credentials/Staging')
    
    function RequestNewAccessToken () {
      try {
        const response = rp({
          method: 'POST',
          url: `${credentials.baseUrl}/oauth/token`,
          form: {
            client_id: credentials.apiKey,
            client_secret: credentials.apiSecret,
            username: credentials.username,
            password: credentials.password,
            grant_type: credentials.grantType
          },
          json: true
        })
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    }
    
    function RefreshAccessToken (token) {
      try {
        const response = rp({
          method: 'POST',
          url: `${credentials.baseUrl}/oauth/token`,
          form: {
            client_id: credentials.apiKey,
            client_secret: credentials.apiSecret,
            grant_type: 'refresh_token',
            refresh_token: token.refresh_token
          },
          json: true
        })
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    }
    
    function RevokeAccessToken (token) {
      try {
        const response = rp({
          method: 'POST',
          url: `${credentials.baseUrl}/oauth/revoke`,
          form: {
            client_id: credentials.apiKey,
            client_secret: credentials.apiSecret,
            token: token.access_token
          },
          json: true
        })
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    }
    
    module.exports = { RequestNewAccessToken, RefreshAccessToken, RevokeAccessToken }
    

    如果执行代码,则通过stdout获得以下文本:

    Debugger attached.
    
        { access_token: '31744bf03a2fb92edb67fcbeead14f4ed8c540843c2439179a54b6439dc94c0e',
          token_type: 'Bearer',
          expires_in: 660,
          refresh_token: 'e53642c69bd0ad954d886dad7a437f88c8c269ecacf2cdcfebc8af1a2d0d9b1e',
          created_at: 1538471914 }
        TypeError: Cannot read property 'refresh_token' of undefined
            at Object.RefreshAccessToken (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/api/adapter/oauth2Adapter.js:28:28)
            at /Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/Main.js:7:28
            at tryCatcher (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/util.js:16:23)
            at Promise._settlePromiseFromHandler (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/promise.js:512:31)
            at Promise._settlePromise (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/promise.js:569:18)
            at Promise._settlePromise0 (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/promise.js:614:10)
            at Promise._settlePromises (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/promise.js:694:18)
            at _drainQueueStep (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/async.js:138:12)
            at _drainQueue (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/async.js:131:9)
            at Async._drainQueues (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/async.js:147:5)
            at Immediate.Async.drainQueues (/Users/quest1onmark/coding_stuff/nodejs/EdgeDeviceAdministration/node_modules/bluebird/js/release/async.js:17:14)
            at runCallback (timers.js:810:20)
            at tryOnImmediate (timers.js:768:5)
            at processImmediate [as _immediateCallback] (timers.js:745:5)
        Waiting for the debugger to disconnect...
    
        Process finished with exit code 0
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Jaromanda X    6 年前

    请尝试以下操作

    const Promise = require('promise')
    const rp = require('request-promise')
    const credentials = require('../../misc/credentials/Staging')
    
    function RequestNewAccessToken () {
        return rp({
          method: 'POST',
          url: `${credentials.baseUrl}/oauth/token`,
          form: {
            client_id: credentials.apiKey,
            client_secret: credentials.apiSecret,
            username: credentials.username,
            password: credentials.password,
            grant_type: credentials.grantType
          },
          json: true
        });
    }
    
    function RefreshAccessToken (token) {
        return rp({
          method: 'POST',
          url: `${credentials.baseUrl}/oauth/token`,
          form: {
            client_id: credentials.apiKey,
            client_secret: credentials.apiSecret,
            grant_type: 'refresh_token',
            refresh_token: token.refresh_token
          },
          json: true
        });
    }
    
    function RevokeAccessToken (token) {
        return rp({
          method: 'POST',
          url: `${credentials.baseUrl}/oauth/revoke`,
          form: {
            client_id: credentials.apiKey,
            client_secret: credentials.apiSecret,
            token: token.access_token
          },
          json: true
        });
    }
    
    module.exports = { RequestNewAccessToken, RefreshAccessToken, RevokeAccessToken }
    

    至于你使用这个的代码-你 console.log(response) oauth2Adapter.RefreshAccessToken(response.body) ... 回应没有身体!

    所以,只要做:

    const oauth2Adapter = require('./api/adapter/oauth2Adapter')
    function test () {
        return oauth2Adapter.RequestNewAccessToken()
        .then(response => oauth2Adapter.RefreshAccessToken(response))
        .then(response => oauth2Adapter.RevokeAccessToken(response))
        .then(console.log)
        .catch(console.log)
    }
    test()
    

    但是,既然你过去了 response

    const oauth2Adapter = require('./api/adapter/oauth2Adapter')
    function test () {
        return oauth2Adapter.RequestNewAccessToken()
        .then(oauth2Adapter.RefreshAccessToken)
        .then(oauth2Adapter.RevokeAccessToken)
        .then(console.log)
        .catch(console.log)
    }
    test()
    
        2
  •  1
  •   shinglesmingles    6 年前

    then return 然后

    oauth2Adapter.RequestNewAccessToken()
    .then(function (requestReponse) {
      console.log(response)
      return oauth2Adapter.RefreshAccessToken()
    })
    .then(function (refreshResponse) {
      return oauth2Adapter.RevokeAccessToken(JSON.parse(refreshResponse.body))
    })
    

        3
  •  0
  •   Estus Flask    6 年前

    承诺应该恰当 锁链 then catch 回调应该返回对chain的承诺,以防有承诺。

    也, response json: true

    ...
    .then(function (response) {
      console.log(response)
      return oauth2Adapter.RefreshAccessToken(response)
    })
    ...