0

I'm pretty new to meteor so this may or may not be the correct behavior although I'm not sure how to 'fix' the example to do what I wish.

I'm creating a chatroom app, to learn Meteor, and when I add messages to the 'chatroom' the messages all send find but don't sort to the correct order until after I refresh the page. The messages are all stored with a timestamps which holds the value Date.parse();

router.js

Router.configure({
  layoutTemplate: 'layout',
  notFoundTemplate: 'notFound',
  loadingTemplate: 'loading'
});

Router.map(function () {
  this.route('home', {
    path: '/',
    template: 'home'
  });

  this.route('room', {
    path: '/room',
    template: 'room',
    waitOn:function(){
        return [Meteor.subscribe('rooms'),
               Meteor.subscribe('messages')];
    },
    data: function() { 
        //console.log(Rooms.find().fetch())
        return {roomUsers: Rooms.find(),
                userMessages: Messages.find({},{sort: {timestamp: 'desc'}})};
    },
    action: function(){
        if(this.ready()){
            this.render();
            console.log(Rooms.find({userId:Meteor.userId()}).count());
            if(Rooms.find({userId:Meteor.userId()}).count() < 1){
                console.log("This happens");
                Rooms.insert({username: Meteor.user().emails[0].address,roomId: 1,userId:Meteor.userId()});
            }
        }
    }
  });
});

room.html

<template name="room">
    <div class="col-xs-10 chatMain">
        <div class="row">
            <div class="col-xs-12 chatContainer">
                {{#each userMessages}}
                    <div class="message">
                        <div class="uname">
                            {{username}}    
                        </div>
                        <div class="text">
                            {{message}}
                        </div>
                    </div>
                {{/each}}
            </div>
            <div class="col-xs-12 colRem">
                <div class="chatbox">
                    <form>
                        <textarea type="text" id="message"></textarea>
                        <button>Send!</button>
                    </form>
                </div>
            </div>
        </div>          
    </div>
    <div class="col-xs-2 chatUsers">
        <ul>
            {{#each roomUsers}}
                <li>{{username}}</li>
            {{/each}}
        </ul>
    </div>    
</template> 

Before refresh This image is before the refresh. After refresh This is what I want to happen automatically each time a message is sent.

Leth0_
  • 544
  • 2
  • 6
  • 15

1 Answers1

0

I figured out the problem, Somehow the date was NaN on clientside although resulted to a correct timestamp on server side, I instead just used the following question to solve my problem,

Jquery Date.parse returning NaN in Chrome browser?

Community
  • 1
  • 1
Leth0_
  • 544
  • 2
  • 6
  • 15