【问题标题】:Trouble Understanding Error Related to Promises and Scope无法理解与 Promise 和范围相关的错误
【发布时间】:2018-06-02 07:01:30
【问题描述】:

我正在为一个粒子光子开发一个小型 Web 前端,我已将其配置为 js 的学习练习。

我有一个错误,我不知道为什么。

代码前面我使用粒子脚本登录https://docs.particle.io/reference/javascript/#with-username-password

var particle = new Particle();
var token;

particle.login({username: 'user@email.com', password: 'pass'}).then(
  function(data) {
    token = data.body.access_token;
  },
  function (err) {
    console.log('Could not log in.', err);
  }
);

在这个例子中,我使用了particle.login(),然后立即使用了.then。 它工作得很好,我得到了我期望的令牌。

接下来我想列出我的所有设备,我使用文档中的示例代码来执行此操作:

var devicesPr = particle.listDevices({ auth: token });

devicesPr.then(
  function(devices){
    console.log('Devices: ', devices);
  },
  function(err) {
    console.log('List devices call failed: ', err);
  }
);

这也很有效。没有问题。

这是我的问题:

我心想“哎呀,我为什么不干脆摆脱这个 var devicesPr 并立即调用它”。所以我尝试:

particle.listDevices({auth: token}).then(
    function(devices){
        console.log('Devices: ', devices);
    },
    function(err) {
        console.log('List of devices call failed: ', err);
    }
);

现在我收到一条错误消息:

init.js:23 List of devices call failed:  {statusCode: 400, errorDescription: "HTTP error 400 from /v1/devices - The access token was not found", error: Error: Unsuccessful HTTP response
    at Request.<anonymous> (http://cdn.jsdelivr.net/particle-api-j…, body: {…}}
init.js:11 API call completed on promise resolve: API KEY

所以我注意到,在生成身份验证令牌之前似乎正在执行对设备列表的请求。我认为这是有可能的,因为 Promise 的文档不断提到它们是异步的。

我只是很困惑,为什么当我先将其设为变量然后调用 .then 时没有看到相同的可能错误?如果我先将承诺存储到变量中,它是否知道等待身份验证令牌存在?

谢谢!

我正在运行的完整故障代码:

"use strict";

//var Particle = require('particle-api-js');
var particle = new Particle();
var token;


particle.login({username: 'MYEMAIL', password:'MYPASSWORD'}).then(
  function(data){
    token = data.body.access_token;
    console.log('API call completed on promise resolve: ', token);
  },
  function(err) {
    console.log('API call completed on promise fail: ', err);
  }
);

particle.listDevices({auth: token}).then(
    function(devices){
        console.log('Devices: ', devices);
    },
    function(err) {
        console.log('List of devices call failed: ', err);
    }
);

【问题讨论】:

    标签: javascript particle.io


    【解决方案1】:

    您没有将listDevices 与从particle.login 获得的token 链接在一起-您正在调用particle.listDevices 同步,在token 被填充之前。获取令牌的请求可能已经发送,但填充 token 的异步 .then 肯定还没有运行。

    改为使用.thenparticle.login 之后链接listDevices,如下所示:

    var particle = new Particle();
    particle.login({username: 'MYEMAIL', password:'MYPASSWORD'})
      .then(data => {
        const token = data.body.access_token;
        console.log('API call completed on promise resolve: ', token);
        // Return the listDevices promise so `.then` can be called on it below:
        return particle.listDevices({auth: token});
      })
      .catch(err => console.log('API call completed on promise fail: ', err))
      .then(devices => console.log('Devices: ', devices))
      .catch(console.log('List of devices call failed: ', err));
    

    【讨论】:

    • 这是有道理的。我将使用您使用的箭头阅读符号。但是,我仍然很困惑为什么立即链接 .then 与首先将其分配给变量会产生不同的结果?我是不是走运了?
    • 不,如果你将它分配给一个变量并在该变量上调用then,它应该也能正常工作。毕竟then(和catch)返回一个Promise。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2016-11-10
    • 2014-04-22
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多