Nearpoint
Nearpoint

Reputation: 7362

In my Meteor application, when I click a link, router does not render the page

I am trying to build a multi-chat room app. I am using router so when a user clicks a link (to chatroom, marked in code) on my site, it routes them to a new template. But when someone clicks a link, the link URL is shown in the address bar, but it does not render the page. But if I visit the link directly, it gets rendered. So basically my links don't go anywhere as they should. Also when I click the link I get an error in console: 'Exception from Deps recompute:' (full error code below)

Here is the code, html first, then Meteor javascript. I commented the relevant parts of the code both in html and js

HTML:

<head>
  <title>testChat</title>
</head>

<body>
  <div class"row">
    {{> room}}
    {{renderPage}}
   </div>
</body>

<template name="home">
  <div class="span6">
    <h1>Welcome to Meteor Chat</h1>
    <p>Please a select a room to chat in, or create a new one</p>
  </div>
</template>

<template name="room">
    <table>
        {{#each rooms}}
        <tr>
            <td><a href="/rooms/{{_id}}">{{name}}</a></td> <!-- THIS IS THE LINK! -->
            <td>{{members}}</td>
            <td>{{last_activity}}</td>
        </tr>
        {{/each}}

         <tr>
          <td colspan="3">
            <input type="text" name="name" placeholder="Enter your chatroom name"></input>
            <button type="submit" class="btn btn-primary pull-right">
              <i class="icon-plus"></i> Create Room
            </button>
          </td>
        </tr>

    </table>
</template>

<template name="chatRoom">
<h1> You are in chat room {{id}} </h1>
</template>

Meteor Javascript:

Rooms = new Meteor.Collection('rooms');

if (Meteor.isServer && Rooms.find().count() == 0) {

  var roomsA = [
    {name: 'Meteor Talk', members: 3, last_activity: '1 minute ago', id: 0, 
      messages: [
        {author: 'Tom', text: 'Hi there Sacha!'},
        {author: 'Sacha', text: 'Hey Tom, how are you?'},
        {author: 'Tom', text: 'Good thanks!'},
      ]},
    {name: 'Meteor Development', members: 2, last_activity: '5 minutes ago', id: 1},
    {name: 'Meteor Core', members: 0, last_activity: '3 days ago', id: 2}
  ];

 for (var i = 0; i < roomsA.length; i++) {
      Rooms.insert(roomsA[i]);
  }; 
}     



if (Meteor.isClient) {

Meteor.Router.add({
  '/': 'home',
  'add': function () { return 'test'},
  '/rooms/:id': function(id) {            //THIS IS THE ROUTER CALL TO THE ROOMS LINKS
    Session.set('currentRoomId', id);
    return 'chatRoom'
  }
});


 Template.room.rooms = function () {
     return Rooms.find();
 };

 Template.chatRoom.id = function() {
        var id = Session.get('currentRoomId', id);
     return id;
 };

console.log(Rooms.find().fetch());


}

How come clicking my links do not render the requested page, but when I visit the link directly it does get rendered?

This is the full console error that shows up when I click a link:

Exception from Deps recompute: _copyAttributes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:494
match@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:249
@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:61
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:17
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
visitNodes@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:18
_patch@http://localhost:3000/packages/spark/patch.js?e76412b922e47b6c2c1f890e3bc10fd13bdecfef:79
@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:639
operate@http://localhost:3000/packages/liverange/liverange.js?5e6bc5ba11645802c3440658c75fcc2277537dd1:458
@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:640
withEventGuard@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:103
renderToRange@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:641
@http://localhost:3000/packages/spark/spark.js?4af332696fb84f1c71f2e678ad0a267755b2b828:864
_compute@http://localhost:3000/packages/deps/deps.js?651e87591167f4286e96438ff2566ba3357bff99:129
_recompute@http://localhost:3000/packages/deps/deps.js?651e87591167f4286e96438ff2566ba3357bff99:142
flush@http://localhost:3000/packages/deps/deps.js?651e87591167f4286e96438ff2566ba3357bff99:224
[native code]

Upvotes: 0

Views: 3457

Answers (1)

Jan Rudovsky
Jan Rudovsky

Reputation: 186

I think, you have just typo in your code.. instead of

<div class+"row"> you wanted <div class="row"> 

.. if you correct this typo, your application will be working.

Maybe one point I think this is better approach to use event handling instead of generating links.

Html https://gist.github.com/rudovjan/6199753

<td><a class="goToRoom" href="#">{{name}}</a></td>

The javascript is here https://gist.github.com/rudovjan/6199750

Template.room.events({
    'click .goToRoom': function(e, t){
      console.log("You clicked");
      console.log(this);
       Meteor.Router.to('/rooms/' + this.id);
     }
  })

Upvotes: 1

Related Questions