0

问题:

当我第一次导入模块时,导出的函数是未定义的。在异常情况下,模块被定义。

为什么它未定义!?!?


设想:

主文件

import { UserClass } from './user';
import { query, Client } from 'faunadb';
import { FaunaClass } from '../class';

console.log('init', typeof UserClass, typeof FaunaClass, typeof query);

export function initialise (client : Client) {

  return new Promise((resolve, reject) => {
    const CLASSES : (typeof FaunaClass)[] = [
      UserClass
    ];

    let count = 0;
    const total = CLASSES.length;

    console.log('classes', CLASSES);

    CLASSES.forEach(cl =>
      client.query(query.CreateClass({ name: cl.className }))
        .then(checkDone)
        .catch(reject));

    function checkDone () {
      count += 1;

      if (total === count) {
        resolve();
      }

    }

  })
    .catch(e => {
      console.log('on catch', UserClass);
      console.log(e.message);
    });

}

如您所见,此函数中有一些控制台日志。上的输出karma start是:

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'init', 'undefined', 'undefined', 'object'

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'classes', [undefined]

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'on catch', function UserClass() { ... }

PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'undefined is not an object (evaluating 'cl.className')'

PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 SUCCESS (0.933 secs / 0.928 secs)

用户类.ts:

import { FaunaClass } from '../class';

export class UserClass extends FaunaClass {

  static className = 'users';

}

** 配置文件:**

业力.conf.js

const webpackConfig = require('./webpack.config');
const webpack = require('webpack');

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai', 'sinon'],

    files: [
      'src/**/*.spec.ts'
    ],

    preprocessors: {
      '**/*.ts': ['webpack', 'sourcemap']
    },

    webpack: {
      module: webpackConfig.module,
      resolve: webpackConfig.resolve,
      devtool: 'inline-source-map'
    },

    // Webpack please don't spam the console when running in karma!
    webpackServer: { noInfo: true },

    reporters: ['progress'],
    colors: true,
    autoWatch: true,
    logLevel: config.LOG_INFO,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: 'Infinity'
  });
};

网络包配置:

var path = require('path');

module.exports = {
  entry: './handler.ts',
  target: 'node',
  module: {
    loaders: [
      {
        test: /\.tsx?$/,
        loaders: ['babel-loader', 'ts-loader'],
        exclude: [/node_modules/]
      },
      { test: /\.json$/, loader: 'json-loader' },
    ]
  },
  resolve: {
    extensions: ['.ts', '.js', '.tsx', '.jsx']
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: 'handler.js'
  }
};

因此,正如您所看到的,UserClass在开始时未定义的日志记录,并且当在 Promise 中引发异常时,该类将变为已定义。

我有一种感觉,由于某种原因,它在导出之前执行代码UserClass,这导致它在那一刻未定义。

那个或我的配置文件完全损坏了。

4

1 回答 1

0

发生这种情况的原因是循环导入循环。我只需要更改代码的运行位置。(或进口顺序)

于 2017-08-11T03:10:54.453 回答