kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 193 forks source link

FlowRouter._routes contain routes but I still get route undefined erro #710

Open NvdB31 opened 7 years ago

NvdB31 commented 7 years ago

I'm looking to initialise routes from pages that are defined in a database collection. So I made the following:

FlowRouter.wait();

Meteor.subscribe("pages", {
    onReady: function () {
        const allPages = Pages.find().fetch();

    for (var i = 0; i < allPages.length; i++) {
        FlowRouter.route('/'+allPages[i].slug, {
            name: allPages[i].name,
            action: function(params, queryParams) {

            }
        });
    };
    }
});

FlowRouter.initialize();

Then when I open up the browser console I can see that the FlowRouter._routes object contains a nice array with all the routes from the collection. However when I visit the routes I get an error: 'There is no route for the path: routename'

Any ideas why this is happening? It seems that it cannot initialise routes from within a loop so it seems?

ziedmahdi commented 7 years ago

@cloudrocketsoftware put FlowRouter.initialize(); inside the onReady callback, at the end.

FlowRouter.wait();

Meteor.subscribe("pages", {
    onReady() {
        Pages.find().forEach(function (page) {
            FlowRouter.route('/' + page.slug, {
                name: page.name,
                action: function (params, queryParams) {

                }
            });
        });

        FlowRouter.initialize();
    }
});