I'm trying to check if a value exists in my rethinkdb table.
It seams like its giving 2 responses.
I do it this way:
router.param('gamename', function(req, res, next, gamename) {
// do validation on gamename here
console.log(gamename);
r.table("Game").filter({
name: gamename,
}).count().eq(1)
.run()
.then(function(response){
if (response) {
console.log('Success ',response);
req.gamename = gamename;
next();
}else{
console.log("Does game exist? : "+response);
res.render('error', { gamename: gamename, message: "Game does not exist" });
}
})
.error(function(err){
console.log('error occurred ',err);
//res.render('error', { });
})
});
For some reason, if I use a gamename
that exists in the table, I get both console.log outputs, from both if and else.
NAMETHATEXISTSINTABLE
Success true
Does game exist? : false
If I use a value that does not exist I get the same twice:
NAMETHATDOESNOTEXIST
Does game exist? : false
Does game exist? : false
Why does it seam like this is run twice? One where the response is always false and the other where it is using my gamename parameter and responding true or false correctly.
Any help will be very appreciated!
I figured it out.
In my app.js
file I defined my routes like this:
var admin = require('./routes/admin');
var main = require('./routes/main');
app.use('/admin', admin); // Add admin routes to middleware chain.
app.use('/', main); // Add game routes to middleware chain.
and in the main.js routes file where I had the problem, gamename was defined:
/* GET game home page. */
router.get('/:gamename', game_controller.index);
/* GET assets routes page. */
router.get('/:gamename/assets', assets_controller.index);
router.get('/:gamename/assets/:id', assets_controller.id);
So since the app.use('/', main);
in app.js
was defined at root level /
the routes where resolved 2 times.
Changing that line to:
app.use('/game', main); // Add game routes to middleware chain.
Fixed the problem. Hovever, now I can't use domaine.com/gamename for each game I want - I need to use domaine.com/game/:gamename.
If anyone has a solution for this, I will be happy to hear about it!