【问题标题】:Request-promise-native not chaining 'then' as expected on working API callsRequest-promise-native 未在工作 API 调用中按预期链接“then”
【发布时间】:2017-11-06 16:51:06
【问题描述】:

“request-native-promise”的使用未正确链接到其后续的“then”和“catch”处理程序。

我的量角器测试

    // There's a bunch of other imports here
    import { browser } from "protractor";
    const RequestPromise = require('request-promise-native');

    describe('spec mapper app', () => {
        let specMapperPage: SpecMapperPage;
        let specMapperFVRPage: SpecMapperFieldsValuesReviewPage;
        let loginLogoutWorkflow: LoginLogoutWorkflow;
        let apiToken: LoginToken;
        let tokenUtil: TokenUtil;
        let projectRecordsToBeDeleted = [];
        let requestHandler;
        let logger = new CustomLogger("spec mapper app");
        let speccyEndpoints = new SpeccyEndpoints();

        beforeAll( () => {
            logger.debug("Before All")
            loginLogoutWorkflow = new LoginLogoutWorkflow();
            loginLogoutWorkflow.login();
            tokenUtil = new TokenUtil();
            tokenUtil.getToken().then((token:LoginToken) => {
                apiToken = token;
                requestHandler = new SpeccyRequestHandler(apiToken);
            });
        });

        describe('import/export page', () => {

            it('TC2962: I'm a test case', () => {
                let testLogger = new CustomLogger("TC2955");
                // Test Var setup
                ... // removed for brevity

                // Test Setup
                browser.waitForAngularEnabled(false);
                // Setup the record to be on the mapper page
                let body = speccyEndpoints.generateRevitPostBody(PROJECT_ID, fileName);

                requestHandler.postToSpeccy(speccyEndpoints.DELITE_REVIT_POST_URI, body).then((response) => { // EDIT: removed non-existant argument "rejection"
                    // --> The then handler the promise is STILL not resolving into
                    // Only made it here like once
                    console.log("Response is: ");
                    console.log(response);

                    // I got this to work once, but now it's not
                    console.log("Response body is: ");
                    console.log(response.body);
                }).catch(error => {
                    // --> The catch handler is ALSO NOT resolving
                    console.log("catch handler executed!");
                    console.log(error);
                });        
            });
        });
    });

出现问题的测试用例。我的console.log("Response is: "); 没有被输出。我没有收到关于原因的错误消息。

我的 Speccy 请求处理程序包装类

import * as RequestPromise from "request-promise-native";
import {LoginToken} from "../testObjects/LoginToken";
import {CustomLogger} from "../logging/CustomLogger";

export class SpeccyRequestHandler {

    _baseAPIURL = 'http://myapi.net/';
    _options = {
        method: '',
        uri: '',
        auth: {
            'bearer': ''
        },
        headers: {
            'User-Agent': 'client'
        },
        "resolveWithFullResponse": true,
        body: {},
        json: true
    };
    _logger;

    constructor(apiToken: LoginToken) {
        this._options.auth.bearer = apiToken.idToken;
        this._logger = new CustomLogger(SpeccyRequestHandler.name);
    }

    getOptions() {
        return this._options;
    }

    postToSpeccy(uri:string, body?) {
        this._options.method = 'POST';
        this._options.uri = this._baseAPIURL + uri;
        if(body) {
            this._options.body = body;
        }
        return RequestPromise(this._options);
    }

    getFromSpeccy(uri) {
        this._options.method = 'GET';
        this._options.uri = this._baseAPIURL + uri;
        return RequestPromise(this._options);
    }
}

这是我的请求处理程序,特定于我的一个 API,即 Speccy 之一,并且在 URL 和令牌传递中具有一些自定义方面。

来源

Request-Promise-Native Github Page
Request-Promise Github page, documentation location

更新编号 1

在修复@tomalak 引起我的注意之后,.then(... 处理程序中的 console.log 正在执行,在我切换到此之后的前 5 次,我得到了响应对象的大约 150 多行控制台日志,其中包含我希望从请求 URI 获得的响应主体。我什至使用response.body 将尸体取出。我认为事情已经解决了,我没有使用我的记录器来注销文件,所以我丢失了我的证明。现在,当我运行这个测试和这个请求时,我根本不会进入 .then(... 处理程序。我也不会陷入困境。我的请求正在工作,因为我的资源是在点击 post 端点时创建的。任何进一步的建议表示赞赏。

什么会导致某些东西有时工作而不是其他工作?我唯一的想法是我的请求处理程序中的通用post 名称可能没有被用来代替被捕获的构建链更高的另一种方法。

第 2 次更新

删除了一堆东西以缩短我的问题。如果您需要更多说明,请询问,我会添加。

【问题讨论】:

  • RequestPromise(options).then((response, reject) ... - 嗯,.then() 回调没有 responsereject 参数...
  • 我明白你的意思了!我是根据我对 Promise 有决心和拒绝的想法来推动它的。我只选择了 .then((response) ... 并得到了实际响应!但我的捕获从未发现在 then 中有额外的参数是一个问题,我从未在其他地方看到错误。有什么我应该做的尝试将这些无声的错误浮到我可以看到的顶部?我很欣赏明显的方向!
  • 这是一个难题。除了使用 Typescript 或类似的东西来追踪你之外,我真的不能推荐任何东西(除了显而易见的 - 当然 - 失败和学习 - 我猜你不会再犯这个错误了)。由于经验,我看到了很多这样的事情,但并不是所有的事情都是从长远来看的。
  • 我正在使用打字稿。并且仍然遇到问题。但肯定会取得某种进展。 :) 我会更新我的问题。

标签: javascript node.js angular typescript protractor


【解决方案1】:

最终导致 API 结束时超时。我的回复只是花了太长时间才回复我。它没有失败,所以它从来没有陷入困境。而且我让它在某个时候工作,因为响应需要这么长时间是由于我们系统中特定资源的过多。我以为是我自己写错了。长话短说,怀疑你的系统,即使你认为它是完美的,或者如果你的开发人员上下发誓,没有任何东西可以被破坏。

此外,request-debug 模块是一件好事,它必须证明其他端点(例如 https://jsonplaceholder.typicode.com/ 的其余测试端点)可以与您的代码一起工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多