-1

我想要实现的是接受对值的请求,将该值添加到数组中,在其上运行一个函数来查询它,然后使用 shift 函数将其从数组中删除。到目前为止,我所拥有的是一个简化的框架,但它现在给了我这个错误。

RangeError:超出最大调用堆栈大小

如果我还可以做些什么来改进我的代码,请告诉我。

应用程序.js

 var express    = require('express')
 , http         = require('http')
 , path         = require('path')
 , fs           = require('fs')
 , eventEmitter = require('events').EventEmitter;

 var app = express();

 app.configure(function(){
 app.set('port', process.env.PORT || 3000);
 app.set('views', __dirname + '/views');
 app.set('view engine', 'jade');
 app.use(express.favicon());
 app.use(express.logger('dev'));
 app.use(express.bodyParser());
 app.use(express.methodOverride());
 app.use(app.router);
 app.use(express.static(path.join(__dirname, 'public')));
 });

 var ee = new eventEmitter;
 var queries = new Array();
 ee.on('next', next_search); 

 function next_search() {
console.log(queries);
search();
 }

 function search() {
    // do something
queries.shift();
console.log(queries);
    ee.emit('next') 
 }

 app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
 });

 app.post('/search', function(req, res) {
letter = req.param('letter');
console.log(letter);
queries.push(letter);
next_search();
 });

 http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port " + app.get('port')); 
 });

jQuery代码

 $(document).ready(function() {
     $('#letter').on("keyup", function() {
      letter = $('#letter').val();
      $.post("/search", { letter: letter}, function(data) {
    $('#status').html(data);
          });
 });
 });
4

1 回答 1

0

当您发布时,这是一个无限函数调用。next_search -> 搜索 -> ee 下一个事件-> next_search -> ...

ee.on('next', next_search); 

 function next_search() {
    console.log(queries);
    search();
 }

 function search() {
    // do something
    queries.shift();
    console.log(queries);
    ee.emit('next') // execute first line
    // only do this when queries is non-empty
 }

您应该检查查询是否为空,然后才执行 next_search。

于 2013-08-03T19:35:53.670 回答