我想要实现的是接受对值的请求,将该值添加到数组,在其上运行一个将查询它的函数,然后使用shift函数将其从数组中删除。到目前为止我所拥有的是一个简化的框架,但它现在给了我这个错误。
RangeError:超出最大调用堆栈大小
如果我有什么办法可以改进我的代码,请告诉我。
app.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);
});
});
});
答案 0 :(得分:0)
当你发帖时,这是一个无限的函数调用。 next_search - >搜索 - > ee next event-> 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。