【问题标题】:Fetch handling promise获取处理承诺
【发布时间】:2017-10-06 20:22:36
【问题描述】:

我正在为我的应用使用此设置:

  • ReactJS
  • KarmaJS
  • 伊斯坦布尔

此时我不断收到此错误: Object is not a constructor (evaluating '(0, _whatwgFetch2['default'])')

在检查网络时,它一直告诉我导入我实际上已经做过的 whatwg-fetch,所以我有点迷失了去哪里。

我执行调用的方法:

import fetch from 'whatwg-fetch';

export function authenticateUser(username, password) {

  return function (dispatch, getState) {
    dispatch({type: 'LOGIN'});

    return fetch(getState().config.components.Authentication.login_endpoint, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      credentials: 'same-origin',
      body: JSON.stringify({
        username: username,
        password: password,
      })
    }).then(resp => {
      if (resp.status === 302) {
        window.location = resp.url;
      } else if (resp.status >= 300 && resp.status < 500) {
        return Promise.resolve(resp);
      } else {
        return Promise.reject(resp.statusText);
      }
    }).then(resp => resp.json())
      .then(resp => {
        const error = data.errors[0];
        dispatch({ type: 'LOGIN_FAILED', payload: error.detail });
    }).catch(error => {
      console.error(error); // I know, still needs to be catched 
    });
  };
}

我的测试用例:

import { expect } from 'chai';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import sinon from 'sinon';
import { authenticateUser } from '../../src/actions/index';

const initialConfig = {
  authentication: {
    loginform_loading: false,
    loginform_errorMessage: null,
    forgotpassword_loading: false,
    forgotpassword_errorMessage: null,
    forgotpassword_passwordSent: false
  },
  config: {
    components: {
      Authentication: {
        'login_endpoint': '/api/auth/login',
        'forgotpassword_enabled': true,
        'forgotpassword_path': '/auth/forgot-password',
        'forgotpassword_endpoint': '/auth/forgot-password'
      }
    }
  }
};

const middlewares = [ thunk ];
const mockStore = configureMockStore(middlewares);
let store;

describe('Action/Index/authenticateUser', () => {

  const testUsername = 'User';
  const testPassword = 'Test123';
  let sandbox;

  beforeEach(function () {
    sandbox = sinon.sandbox.create();
    store = mockStore(initialConfig);
  });

  afterEach(function () {
    sandbox.restore();
  });

  it('LOGIN is dispatched', () => {
    var response = mockResponse(400, 'Found', '{"errors": [{ "message": "first" }] }');
    sandbox.stub(window, 'fetch')
      .resolves(response);

    return store.dispatch(authenticateUser(testUsername, testPassword))
      .then(() => {
        var expectedActions = store.getActions();
        expect(expectedActions).to.not.be.empty;

        var action = expectedActions[0];
        expect(action.type).to.equal('LOGIN');
      });
  });
});

希望有人可以帮助我解决问题。也许还有关于如何改进 Promise 本身的错误处理

谢谢, 皮姆

【问题讨论】:

  • 尝试将您的导入行更改为 import 'whatwg-fetch'
  • 就是这样!之前工作的那个有什么变化吗?

标签: reactjs promise fetch


【解决方案1】:

Panther 对此给出了正确答案。

import 'whatwg-fetch' 成功了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多