Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试获取数据时发出的事件_Javascript_Node.js - Fatal编程技术网

Javascript 尝试获取数据时发出的事件

Javascript 尝试获取数据时发出的事件,javascript,node.js,Javascript,Node.js,我正在尝试创建两个node.js文件。我正在使用事件发射器模块。当我尝试搜索数据时,我希望发出一个事件,以便在从对象数组返回数据之前引发该事件 // event emitter file: var EventEmitter = require('events').EventEmitter; var inherits = require('util').inherits; var _ = require('underscore'); // Custom class functi

我正在尝试创建两个node.js文件。我正在使用事件发射器模块。当我尝试搜索数据时,我希望发出一个事件,以便在从对象数组返回数据之前引发该事件

// event emitter file:
 var EventEmitter = require('events').EventEmitter;
 var inherits = require('util').inherits;
  var _ = require('underscore');

  // Custom class 
  function EmployeeEmitter(args) {
  this.data = args;
  EventEmitter.call(this);
  }
  inherits(EmployeeEmitter, EventEmitter);

  EmployeeEmitter.prototype.lookupByLastName = function (name) {

         this.emit('lookupByLastName',name);

         if( _.where(data, {lastName:name}))
         {
           return _.where(data, {lastName:name})
         }

        else{

                return;
              }
            };
   module.exports.EmployeeEmitter = EmployeeEmitter;
现在这是我的事件发射器,当用户搜索数据时,我想激发lookupbylastName。为了测试它,我在使用event.on方法的地方创建了另一个文件。不幸的是,它没有运行,这给了我一个错误,我不知道哪里出了问题

      //Test file:
         var colors = require('colors');

         var EmployeeEmitter = require('./employeeEmitter').EmployeeEmitter;

          // Usage
       var data = [
        {id:1, firstName:'John',lastName:'Smith'},
        {id:2, firstName:'Jane',lastName:'Smith'},
         {id:3, firstName:'John',lastName:'Doe'}
        ];

      var employees = new EmployeeEmitter(data);
      employees.on('lookupByLastName', function (args) {
     });

     console.log("\nLookup by last name (Smith)".red);
     console.log(employees.lookupByLastName('Smith'));

它给您带来了什么样的错误?如果(u.where(data,{lastName:name}))它说数据没有在我的文件1中定义。如何传递测试文件中定义的数据。