babel es6错误的异常位置与反应

时间:2016-06-09 00:21:21

标签: ecmascript-6 babeljs

我认为这是个问题(不完全确定)。我的javascript控制台抛出的错误总是错误的...无论我的代码中发生错误的位置,它指向我的handleFailure(serviceName,error)块...例如...调用this.foo();手成功后甚至在我移动this.foo();到我的getItemById方法..它总是在同一个块中抛出一个错误...我在我的商店里做错了什么....

如果我删除伪代码它工作得很好...我想向我显示错误引用伪代码..

这是错误:     AircraftLocationStore.js:40服务器调用aircraftLocationRest失败,错误:aircraftLocationRest!handleFailure @ AircraftLocationStore.js:40(匿名函数)@ RestServiceClient.js:20

class AircraftLocationStore extends EventEmitter {
    constructor() {
        super();
        this._populateRestCallStatus = RestCallStatus.NOT_REQUESTED;
        this._dataStore = Map({});
        this.handleSuccess = this.handleSuccess.bind(this);
        this.handleFailure = this.handleFailure.bind(this);
    }
    populate(){
        RestService.fetchData(this.handleSuccess, this.handleFailure, 'aircraftLocationRest');
        this._populateRestCallStatus = RestCallStatus.STARTED
    }

    handleSuccess(serviceName, jsonData){
        UT.logMethod(TAG, `${serviceName} succeeded!`)
        jsonData.forEach((entity) => {
            let tempEntity = AircraftLocationHelper.createFromJson(entity);
            this._dataStore = this._dataStore.merge(Map.of(tempEntity.id, tempEntity))
        });
        UT.log('isMap', Map.isMap(this._dataStore))
        this.foo();
        this._populateRestCallStatus = RestCallStatus.SUCCESS;
        this.emitChange();
    }

    handleFailure(serviceName, error){
        //Utils.logMethod(TAG, 'handleFailure');
        this._populateRestCallStatus = RestCallStatus.FAILED
        console.error(`Server call ${serviceName} failed with error: ${serviceName}!`)
    }

  ...

export default new AircraftLocationStore();

如果我尝试在onChange中更改显示组件上的immutablejs记录,它会告诉我这个......

以防万一我将包含处理总是抛出错误的回调的代码

class RestServiceClient{

    /**
     * @param successCB
     * @param failureCB
     * @param endPoint
     */
    fetchData(successCB, failureCB, endPoint) {
        const serverUrl = BASE_URL + endPoint;
        UT.log('serverUrl', serverUrl);
        fetch(serverUrl).then(r => r.json())
            .then(data => successCB(endPoint, data))
            .catch(e => failureCB(endPoint, e.toString()))
    }
}
export default new RestServiceClient();

这是我的webpack.config

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: "source-map",
  entry: "./src/index.js",
  output: {
    path: __dirname + "/build",
    filename: "bundle.js"
  },

    module: {
        loaders: [{
            test: /\.js$/,
            loaders: ['react-hot', 'babel'],
            include: path.join(__dirname, 'src'),
            exclude: /node_modules/
        }]
    }
};

1 个答案:

答案 0 :(得分:0)

问题似乎出现在由fat arrows =>

创建的匿名函数中

我改写了:

    fetch(serverUrl).then(r => r.json())
        .then(data => let foo = data; successCB(endPoint, data))
        .catch(e => failureCB(endPoint, e.toString()));

为:

fetch(serverUrl)
            .then(
                function(response) {
                    if (response.status !== 200) {
                        failureCB(endPoint, 'Looks like there was a problem. Status Code: ' + response.status);
                    }
                    // Examine the text in the response
                    response.json().then(function(data) {
                        successCB(endPoint, data)
                    });
                }
            )
            .catch(function(err) {
                failureCB(endPoint, 'Looks like there was a problem. Status Code: ' + err);
         }); 

我又一次有了一些有意义的错误消息......

相关问题