React.js - operationStore.getItems不是一个函数

时间:2016-04-03 16:06:26

标签: javascript server gulp babeljs

使用以下命令运行Web App时出现以下错误:node --harmony gulpfile.babel

未捕获的TypeError:operationStore.getItems不是函数

我甚至不知道问题是什么。以下是gulp文件以及发生错误的位置。

在index.ejs文件中声明app.js之外的所有内容时,代码先前有效。但这阻止了我在服务器端文件中使用es6,这是我需要的。所以我死了,使用 - 和谐标志将允许我使用es6。但是当我使用和声标志时,我无法克服这个错误。

getItems对我来说显然是一个功能,发生了什么?

Main.jsx

var Login = require('./auth/LogApp.jsx');
var operationStore = require('./stores/OperationStore.jsx');
var initial = operationStore.getItems();

Gulpfile.babel.js

var gulp = require('gulp');
var LiveServer =  require('gulp-live-server');
var browserSync = require('browser-sync');
var browserify =  require('browserify');
var source = require('vinyl-source-stream');
var babelify = require('babelify');

gulp.task('live-server', function(){
    var server = new LiveServer('server/main.js');
    server.start();
})

gulp.task('bundle',['copy'], function(){
    return browserify({
        entries:'app/main.jsx',
        jquery : 'jquery-browserify',
        debug:true,
    })
        .transform(babelify,
            {
                "presets": ["es2015", "react", "stage-1"],
                "plugins": ["transform-decorators-legacy", "transform-function-bind"],
                extensions: [".jsx", ".js",]
            })
        .bundle()
        .pipe(source('app.js'))
        .pipe(gulp.dest('./.tmp'));
})

gulp.task('copy', function() {
    gulp.src(['app/*.css', 'app/Icons/**/*','app/stores/**/*', 'app/helper/**/*', 'app/dispatcher.js','bundle.js', 'app/helpers/RestHelper.js',
            'app/actions/OperationActionCreator.jsx', 'app/actions/SurgeonActionCreator.jsx',
            'app/actions/PatientActionCreator.jsx','app/actions/ORActionCreator.jsx',
            'node_modules/guid/guid.js','node_modules/jquery/**/*', 'bower_components*/**/*'])
        .pipe(gulp.dest('./.tmp'));
})

gulp.task('serve', ['bundle', 'live-server'], function(){
    browserSync.init(null,{
        proxy:"http://localhost:7777",
        port: 9001
    })
})

gulp.run('serve')

OperationStore.jsx

var operationStore = function() {

    var operations = [];

    var listeners = [];

    var updateOperations = function(helper){
        helper.get("api/operations")
            .then(function(data){
                operations = data;
                triggerListeners(listeners);
            });
    }

    updateOperations(helper);

    function getItems(){
        return operations;
    };

    var addOperationItem = function(operation) {
        operations.push(operation);
        triggerListeners(listeners);
        helper.post("api/operations", operation);
    }

    var deleteOperationItem = function (operation) {
        var index;
        operations.filter(function (_operation, _index) {
            if (_operation.name == operation.name) {
                index = _index;
            }
        });
        operations.splice(operation, 1);
        triggerListeners(listeners);

        //added this
        helper.del('api/operations/'+ operation._id);
    }

    var onChange = function(listener){
        listeners.push(listener);
        return listeners;
    }

    var triggerListeners = function(listeners) {
        listeners.forEach(function(listener) {
            listener(operations);
        })
    }

    var registerEvent = function (event) {
        var split = event.type.split(':');
        if (split[0] === 'operation-item') {
            switch (split[1]) {
                case "add":
                    addOperationItem(event.payload);
                    break;
                case "delete":
                    deleteOperationItem(event.payload);
                    break;
            }
        }
    }

    var dispatchRegister = function(dispatcher) {
        dispatcher.register(registerEvent);
    }

    dispatchRegister(dispatcher);

    return {
        getItems: getItems,
        onChange: onChange,
        addOperationItem: addOperationItem,
        deleteOperationItem: deleteOperationItem,
        triggerListeners: triggerListeners,
        dispatchRegister: dispatchRegister,
        registerEvent: registerEvent,
        updateOperations: updateOperations
    }
}();

1 个答案:

答案 0 :(得分:0)

我知道这是一个简单而又基本的我错过了。 OperationStore.jsx最后需要module.exports。这里有新的jsx。

import RangeDate from './../components/src/range_date'

function operationStore(){
    ...
}

module .exports = new operationStore();