0

I'm having trouble figuring out this issue.

I have a Node/Express server and I'm using MongoDB. Everything is working fine and normally. It has user accounts and the system for login and signup is working normally. I even tested signing up and logging in on my phone (I use Gulp as my build tool).

The problem I'm having now is that whenever I try to signup in the a duplicate tab (as in another tab on the same signup/login page) I get an error:

Server code:

//check for existing account using the given email
User.findOne({ "email" : email }, function(err, doc) {
    if(err) throw err;
    //if no email is found proceed with account creation
    if(!doc) {
        //snip
    } else {
        //if an account is found re-render the page with the error message
        //and close the db
        res.render(...);
        dbase.close();
    }
});

But when I have the same tab open I get this error:

MongoError: server localhost:27017 socket closed.

I've tested this multiple times. It works fine when there are no duplicate tabs. It works on my phone when the tab's still open on the computer. I checked and quadruple-checked to make sure I wasn't closing the DB too soon. This error literally only happens when I have the same tab open. when I submit the form they both start Loading (or attempting to, rather). Here's a screenshot to be very clear:

http://i.gyazo.com/ad89b3df03ba9d44f203563f922893da.png

Again, I've checked multiple times that I'm not closing the DB before receiving a response from the DB, and not outside of that response handle.

Am I missing something in my requests handling? how can I keep it from sending data to both tabs and (seemingly) performing the same operation twice?

Thanks in advance.

Piece Digital
  • 161
  • 1
  • 10

1 Answers1

0

Thanks to Louis93 I have better clarity on how to construct my server an database queries.

The connection must be reused! More information here: https://stackoverflow.com/a/14607887/765409 i.e. Don't close it

Community
  • 1
  • 1
Piece Digital
  • 161
  • 1
  • 10