Query runs before subscription is ready

Hi,

I have a case where I’m defining a local collection based on an existing mongo collection. The below part of code is common between client and server:

import Tabular from 'meteor/aldeed:tabular';

localCollection =  new Meteor.Collection(null);

var data = alerts.findOne({alertCategory :"applicationProtocolViolation"})["unknownApplicationProtocols"]
console.log("alerts.findOne...");
console.log(data);

if (data) {
    localCollection.find().forEach(function(x) { localCollection.remove(x._id) });
    for (var i=0;i<data.length; i++) {
        localCollection.insert({
            client: data[i].client,
            server: data[i].server,
            port: data[i].port
        });
    }
}
console.log("localCollection: ");
console.log(localCollection.findOne());

new Tabular.Table({

    name: "listApplicationProtocolViolationTable",
    collection: localCollection,
    autoWidth: false,
    columns: [

        {
            data: 'client',
            title: 'Client',
        },
        {
            data: "server",
            title: "Server"
        },
        {
            data: "port",
            title: "Port (protocol)"
        }

    ]
});

On my client side router (iron router) I subscribe to alerts collection using the waitOn function.

...
waitOn: function() {
        return [
            Meteor.subscribe('alerts','open')
        ];
    },
...

If you look at the common code, there is an alerts.findOne query. This works well on the server side. However, on the client side, it gets triggered even before the route’s waitOn can return the subscription, thereby, I always get an undefined when the query executes.

Now, I know that I can implement a template level subscription instead of router level subscription and have a subscription.onReady() to make sure I wait until the subscription is ready. How do I go about doing this when the code is common to both client and server?