ReferenceError: Assets is not defined

Hello everybody,

I’m stuck on a litlle problem

Before showing code, I must say that i’m a beginners in meteor and web developpement ( I’m in network and system ) and sorry for my bad english I’m speak french.

Now to the problem

I’m making a little application to monitor my openvpn connection.
To do this i must open and parse a text file.

When I use the Assets.getText(“file”), my client browser get an exception but code does what it should.

Here is the error

ReferenceError: Assets is not defined
var linesGet = Assets.getText('openvpn-status.log');

Here is the code from openvpn.js

connectionList = new MysqlSubscription('allInfo');
vpnUserList = new MysqlSubscription('vpnInfo');
vpnUserHistory = new MysqlSubscription('oneUserHistory','TEST');
fileContent = new Mongo.Collection('fileContent');


Accounts.config({
    forbidClientAccountCreation: true
});

if (Meteor.isServer) {

  // This code only runs on the server


 var liveDb = new LiveMysql({
    host: 'localhost',
    // Port 3407 as specified in leaderboard.mysql.json
    // If using external MySQL server, the default port is 3306
    port: 3306,
    user: 'test',
    password: 'test',
    database: 'connection'
  });

  var closeAndExit = function() {
    liveDb.end();
    process.exit();
  };
  // Close connections on hot code push
  process.on('SIGTERM', closeAndExit);
  // Close connections on exit (ctrl + c)
  process.on('SIGINT', closeAndExit);

  Meteor.publish('allInfo', function() {
    return liveDb.select(
      'SELECT * FROM connection ORDER BY time DESC limit 5',
      [ { table: 'connection' } ]
    );
  });
  Meteor.publish('vpnInfo', function() {
    return liveDb.select(
      'SELECT DISTINCT common_name FROM connection ORDER BY common_name ASC',
      [ { table: 'connection' } ]
    );
  });
  Meteor.publish('oneUserHistory',function(vpnUser) {
    return liveDb.select(
      'SELECT * FROM connection where common_name = ' + liveDb.db.escape(vpnUser)+ ' ORDER BY time DESC',
      [ { table: 'connection' } ]
    );
  });
  Meteor.publish("fileContent", function () {

    return fileContent.find();

  });

}



if (Meteor.isClient) {

  // This code only runs on the client

  accountsUIBootstrap3.setLanguage('fr');

  Meteor.subscribe("fileContent");

  Template.registerHelper('formatDate', function(date) {
    return moment(date).format('DD/MM/YYYY  HH:mm:ss');
  });
  Template.registerHelper('vpnUserList', function() {
    return vpnUserList.reactive();
  });
  Template.registerHelper('connectionList', function() {
    return connectionList.reactive();
  });
  Template.registerHelper('vpnUserHistory', function() {
    return vpnUserHistory.reactive();
  });
  Accounts.ui.config({

    passwordSignupFields: "USERNAME_ONLY"

  });

  Template.body.created = function() {
    Session.set("activeTemplate", "lastHistory");
    Session.set('selectedUser', "none");

  }
  Template.body.helpers({
    activeTemplate: function() {
     return Session.get('activeTemplate');
   }
  });

  Template.body.events({
    'click .real-time': function(){
      if ( Session.get("activeTemplate") != "realTime") {
         Session.set("activeTemplate", "realTime");

         Meteor.call('readFile');
         Session.set('intervalId',Meteor.setInterval(function(){ Meteor.call('readFile');},60*1000));
         }

      else {
         Session.set('selectedUser', "none");
         Session.set("activeTemplate","lastHistory");
         Meteor.clearInterval(Session.get("intervalId"))
         }


    }
  });


  Template.userTmplList.helpers({
    vpnUserNumber: function() {
        if (vpnUserList.length > 0 ) {
                return true;
        }
        else {
                return false;
        }

    }
  });

 Template.userVpnItem.events({

   'click a': function(){
      var nameUser = this.common_name;
      if ( nameUser == Session.get('selectedUser')) {
         Session.set('selectedUser', "none");
         Session.set("activeTemplate", "lastHistory");
         }

      else {
         Session.set('selectedUser', nameUser);
         vpnUserHistory.change(nameUser);
         Session.set("activeTemplate","userHistory");

         }

   }

  });
 Template.userVpnItem.helpers({

   'isSelected': function(){
      var userName = this.common_name;
      var selectedUser = Session.get('selectedUser');
      if(userName == selectedUser){
          return "selected"
      }
    }
  });
 Template.realTime.helpers({
   fileContent: function() {

    return fileContent.find();

   }
 });
}

Meteor.methods({
    readFile : function() {
      var linesGet = Assets.getText('openvpn-status.log');
      fileContent.remove({});
      var lines= linesGet.split(/\r\n|\n/);
      var l = lines.length - 1;
      var i = 3;
      while( lines[i] != "ROUTING TABLE" && i < l ) {
       var line = lines[i];
       var line_parts = line.split(',');
       fileContent.insert ({
         common_name : line_parts[0],
         real_address: line_parts[1],
         bytes_received: line_parts[2],
         bytes_send: line_parts[3],
         connected_since: line_parts[4]
         });
       console.log("Comming from insert : "+line_parts[0]);
       i++;

      return true;

      }
    }
});

Thanks for your help.

Quoting docs.meteor.com:

Assets allows server code in a Meteor application to access static server
assets, which are located in the private subdirectory of an application’s
tree. Assets are not processed as source files and are copied directly
into your application’s bundle.

It works because it is defined on the server and throws an error on the client because it is not defined on the client.

I understand this

but my Assets is in a Meteor.methods

and I call it from client function with Meteor.call(‘readFile’);

It isn’t how I should do it ?

It looks like you have defined your methods in shared code, which means it will exist and be executed on both the client and the server. That is not a bad thing, as it permits Optimistic UI for Meteor’s calls/methods, but as assets are only available on the server the client will throw an error when it tries to execute them. Either define your method on the server only (for example in a file in a server/ folder) or wrap the offending code in a Meteor.isServer block.

ist your meteor methods placed in client and server code? If it is placed in common code, I think you could put this into a this.isSimulation. See http://docs.meteor.com/#/full/method_issimulation

1 Like

Thanks

I have put my code in my Meteor.isServer block and it’s good now.

Thanks you.

I must understand better Meteor concept about sharing code.