Javascript Meteor.users.findOne的问题
我目前正在开发一个应用程序,其中我构建了聊天功能 我目前有一种非常有限的创建对话的方法,在此期间,创建对话的用户和指定的目标用户都将其ID放入对话集合中的对话文档中 我目前正试图在包含每个ID的数组中循环,并在找到与当前用户不匹配的ID后,获取其他用户的配置文件信息Javascript Meteor.users.findOne的问题,javascript,meteor,Javascript,Meteor,我目前正在开发一个应用程序,其中我构建了聊天功能 我目前有一种非常有限的创建对话的方法,在此期间,创建对话的用户和指定的目标用户都将其ID放入对话集合中的对话文档中 我目前正试图在包含每个ID的数组中循环,并在找到与当前用户不匹配的ID后,获取其他用户的配置文件信息 otherUserName: function(){ for ( i = 0; i < this.users.length; i++ ) { if( this.users[i].id !== Meteo
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 }));
}
}
}
otherUserName:function(){
对于(i=0;i
但是,目前它没有输出任何内容。它只显示为未定义
你知道我需要做什么才能让名字显示出来吗?,但在服务器端。当您需要客户端上的用户集合时,您必须发布并订阅它
我在服务器端有一个publication.js
Meteor.publish("users", function () {
return Meteor.users.find();
});
以及lib文件夹中的router.js(用于客户端和服务器),其中包含
用户集合现在应该可以在客户端中访问。可能尝试将发布函数限制为几个属性,而不是全部属性。答案是将返回值转换为字符串:
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;
}
}
},
otherUserName:function(){
对于(i=0;i
几个问题:您是否已发布(并订阅)用户收藏?this.users[i].id是否在循环中定义?当您在for循环中点击一个不同的id时,是否真的要返回
?我当前启用了autopublish,确实如此!只有两个用户,所以我只需要在它找到一个不是当前用户时返回。当您输入Meteor.users.find()
或Meteor.user()时
在Chrome/浏览器控制台中,结果是什么?我可以想象的唯一原因是this.users[I].id
以某种方式包含null
或未定义的
或不正确的值。对于明显的建议表示歉意,但是如果您要查找的ID确实存在,您可以使用meteor mongo
客户端进行检查。此外,请检查您为findOne方法提供的ID类型(ObjectID或字符串)。是的,ObjectID当时是:)我想您仍然必须发布用户,因为这是一个“特殊”集合,但我目前无法获取。@deftone不是根据文档:如果安装了autopublish软件包,有关系统上所有用户的信息将发布到所有客户端
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;
}
}
},