连接已创build但未find

我正在开发一个使用Node JS服务器和firebird数据库的Cordova应用程序。 这两个数据库存在,我可以通过isql-fb连接上它。

当我通过电话创build第一个连接时,连接工作,以及在这个连接上完成的查询。

但是,当我创build第二个连接时,它不起作用。

这是我的代码:

连接表,将连接重新组合为[“username”,connexion]表

var connectionTable = []; 

连接到数据库并在连接表中添加连接的function:

 function connectionToDB(username, password, dbAddress){ var con = fb.createConnection(); con.connectSync(dbAddress, username, password, ''); var newConnection = [username, con]; connectionTable.push(newConnection); console.log('connection finished'); //it is displayed for both connections so I think it works }; 

通过input的用户名返回想要的连接的函数:

 function getConnection(username){ for(x in connectionTable){ if(connectionTable[x][0]==username) { return connectionTable[x][1]; } else {console.log("No connection found with username : " + username); return 0;} } 

};

然后,在我的节点JS服务器,我有这个“路线”:

 app.get('/checkAccount/:username', function(req, res){ console.log("checkAccount avant body"); console.log(req.params); var usernameAsking = req.params.username; console.log("usernameAsking = " + usernameAsking); //returns username in both cases var connexionLinked = getConnection(usernameAsking); console.log(typeof connexionLinked); console.log(connexionLinked); if(connexionLinked == 0) console.log("Problem. No linked connexion found"); else console.log("connexion found"); var query = connexionLinked.querySync("SELECT * from shops"); var rows = query.fetchSync('all', true); console.log(sys.inspect(rows)); var shopNameString = JSON.stringify(rows); console.log("string : " + shopNameString); res.send(shopNameString); }); 

所以对于数据库的第一次连接,它工作得很好,并且返回数据库的值。

但是,它不适用于第二个不同的连接,它说它等于0,所以我认为它没有在GetConnection中find它。

我不明白为什么它适用于一个,而不是另一个…我也尝试连接到几个不同的连接,而不是这个过程,它的工作原理。 所以问题不是我们不能在同一个NodeJS服务器上有多个连接,但在这种情况下,我们无法find好的显然…

有人知道我在做什么错吗?

任何帮助将非常感激,谢谢。

– 编辑 –

连接以这种方式工作。 错误发生在getConnection(用户名)的for循环中。 我让这个post在这里,但如果有人试图做一个连接表。

谢谢你们。

我发现了这个问题。 我觉得我好笨。 但是现在我们知道连接的创build就是这样工作的。 这是最没有人能做的最尴尬的错误:

 function getConnection(username){ for(x in connectionTable){ console.log("connectionTable : " + connectionTable[x]); console.log("connectionTable FULL: " + connectionTable); console.log("connectionTable[x][0] : " + connectionTable[x][0]); console.log(connectionTable.length); if(connectionTable[x][0]==username) { return connectionTable[x][1]; } } console.log("No connection found with username : " + username); return 0; // Error solved : The return was not supposed to be in the for loop //it was only checking the first username value. }; 

谢谢大家的时间和帮助:)