onBeforeAction is executed twice!


#1

Hi,
I’m trying to build an app.
This app should force the user to create a password for ‘admin’ user in its first run
I’m trying to implement this in onBeforeAction, but It gets executed twice.

var isFirstRun = function() {
	var admin = Meteor.users.findOne({ username: 'admin' }).username;

	if (admin) {
		console.log('Admin exists');
		this.next();

	} else {
		console.log('This is a first run');
		this.render('firstRun')
	}

}


Router.onBeforeAction(isFirstRun);


// Browser Console Output
Exception in callback of async function: TypeError: Cannot read property 'username' of undefined
    at isFirstRun (http://localhost:3000/lib/router.js?917bd3d2a86483a854caf15cfd5a37d81198a239:23:57)
    at http://localhost:3000/packages/iron_router.js?7b16fe70bccff9182aca02afb2ccd7708ac57c64:1209:36
    at _.extend.withValue (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:955:17)
    at Router.addHook.hookWithOptions (http://localhost:3000/packages/iron_router.js?7b16fe70bccff9182aca02afb2ccd7708ac57c64:1208:27)
    at boundNext (http://localhost:3000/packages/iron_middleware-stack.js?56e0c02a758f48c0a704ca978a8fb6d7bd9ebbab:424:31)
    at Meteor.bindEnvironment (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:983:22)
    at onRerun (http://localhost:3000/packages/iron_router.js?7b16fe70bccff9182aca02afb2ccd7708ac57c64:521:9)
    at boundNext (http://localhost:3000/packages/iron_middleware-stack.js?56e0c02a758f48c0a704ca978a8fb6d7bd9ebbab:424:31)
    at Meteor.bindEnvironment (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:983:22)
    at onRun (http://localhost:3000/packages/iron_router.js?7b16fe70bccff9182aca02afb2ccd7708ac57c64:508:9)
Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?
Admin exists

#3

I cannot answer your question. Maybe an Iron Router specialist will.
However, if you are new to Meteor, you might consider not using Iron Router, or at least not using Iron Router hooks. They are very difficult to master. The best way to begin with Meteor is to not use any router at all. If really you need one, use Flow Router, which is simpler.


#4

I don’t understand, Is there something wrong in my post?


#5

Ok @Steve, I will read about Flow Router. I just learned from Discover Meteor book and needed to apply some ideas to get experience.
Thank you.


#6

Got it, you mean that I didn’t categorized my post.
Sorry some other forums don’t provide the Category ‘help’.
:smile: