"Cannot read property 'route' of undefined" in Meteor iron:router package

I am new to Meteor, i need to create post end point in my meteor application that’s why i added iron:router package in application by

meteor add iron:router

iron:router is added successfully and visible in package list i have checked using

meteor list

here is Screenshot of part of list :

and importing it using

 import { Router } from 'meteor/iron:router';

when i am using this package for creating end point

Router.route('/testroute', {where: 'server'}).post(function(){
  var response;
  if(this.request.body.userName === undefined || this.request.body.userPassword === undefined) {
      response = {
          "error" : true,
          "message" : "invalid data"
  } else {
      console.log(" Request Body is : ",this.request.body);
      response = {
          "error" : false,
          "message" : "User added."

Now my code is not compiling and give an exception

Exception in callback of async function: TypeError: Cannot read property 'route' of undefined

Please help me if i am doing something wrong or needed any other configuration. Thanks in advance :slight_smile:

is there nobody to help me ? i’m waiting :confused::pensive::disappointed:

I just copied and pasted your example into my code and it works. Can you try when

import { Router } from 'meteor/iron:router';

is commented out?

Oh, and you need to tell Iron Router that this is a server route

Router.route('/testroute', {where: 'server'} ... );

If i commented out then i get exception -

Exception in callback of async function: ReferenceError: Router is not defined

yes, you are right and i updated my code but same issue :frowning:

Are you somehow redefining the global Router somewhere? As I said, the code works in my test here, even when not importing Router.

I found this

Router = Package['iron:router'].Router;

in global-imports.js file. My global-imports.js files looks like

/* Imports for global scope */

check = Package.check.check;
Match = Package.check.Match;
DDPCommon = Package['ddp-common'].DDPCommon;
EJSON = Package.ejson.EJSON;
FastClick = Package.fastclick.FastClick;
HTTP = Package.http.HTTP;
$ = Package.jquery.$;
jQuery = Package.jquery.jQuery;
Log = Package.logging.Log;
Mongo = Package.mongo.Mongo;
Random = Package.random.Random;
RateLimiter = Package['rate-limit'].RateLimiter;
ReactiveDict = Package['reactive-dict'].ReactiveDict;
ReactiveVar = Package['reactive-var'].ReactiveVar;
Reload = Package.reload.Reload;
ServiceConfiguration = Package['service-configuration'].ServiceConfiguration;
Session = Package.session.Session;
Spacebars = Package.spacebars.Spacebars;
Tracker = Package.tracker.Tracker;
Deps = Package.tracker.Deps;
DelayedTask = Package['konecty:delayed-task'].DelayedTask;
UserPresence = Package['konecty:user-presence'].UserPresence;
UsersSessions = Package['konecty:user-presence'].UsersSessions;
Status = Package['francocatena:status'].Status;
Gravatar = Package['jparker:gravatar'].Gravatar;
BlazeLayout = Package['kadira:blaze-layout'].BlazeLayout;
FlowRouter = Package['kadira:flow-router'].FlowRouter;
SandstormAccounts = Package['kenton:accounts-sandstorm'].SandstormAccounts;
ReactiveStore = Package['mrt:reactive-store'].ReactiveStore;
Helpers = Package['raix:handlebar-helpers'].Helpers;
slugify = Package['yasaricli:slugify'].slugify;
Meta = Package['yasinuslu:blaze-meta'].Meta;
TAPi18next = Package['tap:i18n'].TAPi18next;
TAPi18n = Package['tap:i18n'].TAPi18n;
_ = Package.underscore._;
Router = Package['iron:router'].Router;
RouteController = Package['iron:router'].RouteController;
Accounts = Package['accounts-base'].Accounts;
Facebook = Package['facebook-oauth'].Facebook;
Github = Package['github-oauth'].Github;
Google = Package['google-oauth'].Google;
MeteorDeveloperAccounts = Package['meteor-developer-oauth'].MeteorDeveloperAccounts;
Twitter = Package['twitter-oauth'].Twitter;
Blaze = Package.ui.Blaze;
UI = Package.ui.UI;
Handlebars = Package.ui.Handlebars;
Template = Package['templating-runtime'].Template;
meteorInstall = Package.modules.meteorInstall;
meteorBabelHelpers = Package['babel-runtime'].meteorBabelHelpers;
Promise = Package.promise.Promise;
Meteor = Package.meteor.Meteor;
global = Package.meteor.global;
meteorEnv = Package.meteor.meteorEnv;
WebApp = Package.webapp.WebApp;
DDP = Package['ddp-client'].DDP;
LaunchScreen = Package['launch-screen'].LaunchScreen;
OAuth = Package.oauth.OAuth;
Oauth = Package.oauth.Oauth;
LinkedIn = Package['pauli:linkedin-oauth'].LinkedIn;
Iron = Package['iron:core'].Iron;
HTML = Package.htmljs.HTML;
Autoupdate = Package.autoupdate.Autoupdate;

I have tried Picker as well for server side routing but getting same error like

Exception in callback of async function: ReferenceError: Picker is not defined


Considering that it works in my code without the re-definitions as seen in your global-imports.js file, I would start looking there.

Another thing I notice is that you use Iron:Router as well as FlowRouter? I don’t think that will work.

Your setup seems rather unorthodox and I recommend you consult the Meteor Guide

Yes i also have used flow-router for client side routing its working fine but for server side routing i’m trying iron-router. Any way thanks @jamgold for your support and time. I need to consult with meteor guide. :smile:

I would recommend using Meteors WebApp package for server side routing, it is very similar to Node.js request package, and works great with other routers:

Thank you @vyvegard , i’ll try this as i still not found solution :slight_smile: