Question

I am following this meteor chat tutorial and I am halfway, the teacher finishes a piece of code and checks it out in the console in the browser and says good "no errors" but I am getting a error a the same point. Any help as to why I am getting this error would be great.

file.js

Message = new Meteor.Collection('messages');

if (Meteor.is_client){
  Template.messages.messages = function () {
    return Messages.find({}, { sort: {time: -1} });
  };
}

file.html

<head>
  <title>tutorial</title>
</head>

<body>
 {{> entry}}

 {{> messages}}
</body>

<template name="entry">
    <p>
    <input type="text" id="name" placeholder="your name">
    <input type="text" id="messageBox" placeholder="your message"></p>
</template>

<template name="messages">
    {{#each messages}}
        {{> message}} <!--echo of message template-->
    {{/each}}
</template>

<template name="message">
    <p><strong>{{name}}:</strong>{{message}}</p>
</template>

Error

Uncaught ReferenceError: Messages is not defined

UPDATE

Doing the following message = return ... gives me the output below

Your app is crashing. Here's the latest log.


app/tutorial.js:5
     message = return Messages.find({}, { sort: {time: -1} });
               ^^^^^^
SyntaxError: Unexpected token return
    at /Users/anderskitson/Sites/tutorial/.meteor/local/build/server/server.js:113:21
    at Array.forEach (native)
    at Function._.each._.forEach (/usr/local/meteor/lib/node_modules/underscore/underscore.js:79:11)
    at run (/Users/anderskitson/Sites/tutorial/.meteor/local/build/server/server.js:99:7)
Exited with code: 1
Your application is crashing. Waiting for file change.
Was it helpful?

Solution

This line:

Message = new Meteor.Collection('messages');

declares a variable called Message. So, later on, you can use the variable Message.

This line:

return Messages.find({}, { sort: {time: -1} });

uses the variable Messages, which is not defined! Only Message is defined. So, write

return Message.find({}, { sort: {time: -1} });

instead.

OTHER TIPS

You need to check Meteor.isClient before

if (Meteor.isClient) {  
  alert(); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top