![](/img/trans.png)
[英]Meteor - missing username Meteor.users.findOne(); or Meteor.users();
[英]Issues with Meteor.users.findOne
我目前正在開發一個應用程序,其中已建立了聊天功能。
目前,我創建對話的方式非常有限,在此過程中,創建對話的用戶以及指定的目標用戶都將其ID放入了“對話”集合中的“對話”文檔中。
我目前正在嘗試遍歷包含每個ID的數組,並在找到與當前用戶不匹配的ID時獲取其他用戶的個人資料。
otherUserName: function(){
for ( i = 0; i < this.users.length; i++ ) {
if( this.users[i].id !== Meteor.user()._id){
return( Meteor.users.findOne({ _id: this.users[i].id }));
}
}
}
但是,當前它不輸出任何內容。 它僅顯示為未定義。
有什么線索可以顯示名字嗎?
我有一個類似的問題 ,但在服務器端。 當您需要客戶端上的用戶集合時,您必須發布它並訂閱它。
我在服務器端有一個publication.js
Meteor.publish("users", function () {
return Meteor.users.find();
});
在lib文件夾(用於客戶端和服務器)中的router.js與
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
waitOn: function() { return [Meteor.subscribe('events'), Meteor.subscribe('users')]; }
});
用戶集合現在應該可以在客戶端中訪問。 也許嘗試將發布功能限制為幾個屬性,而不是全部。
答案全是將返回值轉換為字符串:
otherUserName: function(){
for ( i = 0; i < this.users.length; i++ ) {
if( this.users[i].id !== Meteor.user()._id){
var user = String(this.users[i].id);
return Meteor.users.findOne({ _id: user }).profile.name;
}
}
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.