Meteor: How can I publish and subscribe a user avatar from the public folder?

时间:2016-02-12 21:41:36

标签: meteor username avatar

Im creating an instant messenger app and I am wondering how I can access the users avatar from the public folder and username from the collection. I would like the username and avatar to appear next to the users message. I keep getting a broken image and [object Object]. Object Object, should be username with the avatar. Check out this image.

Template

\r\n

Client Side

<template name="chat_message">
    <div class = "container">
        <div class = "row">
            <img src="/{{profile.avatar}}" class="avatar_img">
            {{username}} said: {{text}} 
        </div>
    </div>  
    <br>
</template>

Server Side

Meteor.subscribe("userData");
Meteor.subscribe("users");

Template.chat_message.helpers ({
    username: function() {
      var userId = this.userId;
      var user = Meteor.users.findOne(userId);
      var username = user && user.profile && user.profile.username;
      var avatar = user && user.profile && user.profile.avatar;

      return {
        username: username,
        avatar: avatar
      } 
    },
  });

  Template.chat_page.helpers({
    messages:function(){
      var chat = Chats.findOne({_id:Session.get("chatId")});
      return chat && chat.messages;
    }, 
    other_user:function(){
      return ""
    }, 

  });

2 个答案:

答案 0 :(得分:1)

我这样解决了: 您可以像这样在msg数组中插入用户名和头像...

Template.chat_page.events({
 // this event fires when the user sends a message on the chat page
 'submit .js-send-chat':function(event){
   // stop the form from triggering a page reload
   event.preventDefault();
   // see if we can find a chat object in the database
   // to which we'll add the message
   var chat = Chats.findOne({_id:Session.get("chatId")});
   if (chat){// ok - we have a chat to use
     var msgs = chat.messages; // pull the messages property
     if (!msgs){// no messages yet, create a new array
       msgs = [];
     }
     // push adds the message to the end of the array
     var user = Meteor.user().profile.username;
     var avatar = Meteor.user().profile.avatar;
     msgs.push({text: event.target.chat.value, user:user, avatar:avatar});
     // reset the form
     event.target.chat.value = "";
     // put the messages array onto the chat object
     chat.messages = msgs;

     // update the chat object in the database.
     Chats.update(chat._id, chat);
   }
 }
})

模板.....

<template name="chat_message">
  <img src="/{{avatar}}" alt="user" class="chat-img" />
  {{user}}:  {{text}}
  <br />
</template>

答案 1 :(得分:0)

This is the code I used for a similar task:

Server:

func addNumbers(numbers: Int ...) -> Int {
    var total : Int = 0
    for number in numbers {
        total += number
    }
    return total
}

func multiplyNumbers(numbers: Int ...) -> Int {
    var total : Int = numbers [0]
    for (index,number) in numbers.enumerate()  {
        if index == 0 {
            continue
        }
        total *= number
    }
    return total
}

func mathOperation(mathFunction: (Int ...) -> Int, numbers: Int ...) -> Int {
     return mathFunction(numbers)
}

Methods:

Meteor.publish("chats", function(){

    return Chats.find({$or:[
                {user1Id:this.userId}, 
                {user2Id:this.userId}
                ]});
  });

Meteor.publish("users", function(){

    return Meteor.users.find();

  });

Helpers:

Meteor.methods({

    addChat:function(otherUserId){
        var currentUser = this.userId;
        var otherUser = otherUserId;
        var id = Chats.insert({user1Id:currentUser, user2Id:otherUser});
        return id;
    }, //end of method for adding chat items    


    addMessage:function(chat){
        Chats.update(chat._id, chat);
    },//end of method for adding a message

});