2016-02-12 140 views
1

即時通訊創建一個即時通訊應用程序,我想知道如何從公共文件夾和用戶名集合中訪問用戶頭像。我希望用戶名和頭像出現在用戶消息旁邊。我不斷得到一個破碎的圖像和[對象對象]。對象對象,應該是頭像的用戶名。 Check out this image.流星:我如何從公共文件夾發佈和訂閱用戶頭像?

模板

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

客戶端

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 "" 
    }, 

    }); 

服務器端

Meteor.publish("userData", function(){ 
    return Meteor.users.find({_id: this.userId}); 
}); 

Meteor.publish("users", function(){ 
    return Meteor.users.find({ "status.online": true }) 
}); 

    Meteor.methods({ 
    sendMessage: function (chat) { 
     Chats.insert({ 
     chat: chat, 
     createdAt: new Date(), 
     username: Meteor.user().profile.username, 
     avatar: Meteor.user().profile.avatar, 
     }); 
    }, 
    }); 
+0

您使用的是包角色創造/整合?如果是這樣,哪一個? – MasterAM

+0

不,我沒有使用任何軟件包。最好使用一個? – Jessica

+0

這取決於你,但是你的代碼有幾個問題。其中一些是通過答案解決的(儘管我不會推薦給出的所有建議)。請分享一個用戶對象的樣子,特別是它的簡介。仔細想想你的數據 - 你想在每個聊天消息和每次聊天中存儲什麼內容。首先,如果用戶名和頭像存儲在聊天消息中,則不需要「用戶」集合即可獲取它。 – MasterAM

回答

0

這是我用類似的任務代碼:

服務器:

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

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

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

    return Meteor.users.find(); 

    }); 

方法:

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 

}); 

助手:

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

    }) 


    Template.chat_message.helpers({ 
    user:function(){ 
    var currentId = Session.get('chatId'); 
    console.log(Chats.findOne({_id:Session.get("chatId")})); 
    console.log(Meteor.user()._id); 
    return Meteor.user()._id; 
     }, 
    }); 
+0

謝謝,我試過了,但沒有奏效。我是否也應該對方法進行更改? – Jessica

+0

我正在編輯答案,只是試圖複製我實現這一點的方式。 – StefanL19

+0

謝謝,我能夠得到它的工作。 – Jessica

1

我解決這樣的: 可以在味精陣列像這樣插入用戶名和頭像...

而且t emplate .....

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