[英]Meteor.users access with string argument
我正在尝试列出项目团队的成员,这些成员作为userIds存储在projectMembers数组中。 我使用{{#each projectMember}}遍历当前成员以显示其用户名(如果存在)或电子邮件地址。 不幸的是,我收到一个我无法调试的错误。
控制台日志显示适当的用户标识已作为字符串元素发送到Meteor.users.findOne(),但控制台上显示错误。
String {0: "o", 1: "o", 2: "T", 3: "w", 4: "4", 5: "T", 6: "i", 7: "5", 8: "j", 9: "u", 10: "L", 11: "5", 12: "v", 13: "B", 14: "F", 15: "X", 16: "r", length: 17} createProject.js?5f1e645071dc1f451f671ec1d93aa9051ebadc0b:26
Exception from Deps recompute function: TypeError: Object 0 has no method 'substr'
at http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1370:13
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:159:22)
at compileDocumentSelector (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1369:5)
at _.extend._compileSelector (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1346:12)
at new Minimongo.Matcher (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:1289:27)
at new LocalCollection.Cursor (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:142:20)
at LocalCollection.find (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:125:10)
at LocalCollection.findOne (http://localhost:3000/packages/minimongo.js?4ee0ab879b747ffce53b84d2eb80d456d2dcca6d:188:15)
at _.extend.findOne (http://localhost:3000/packages/mongo-livedata.js?cf17a2975aa7445f0db2377c2af07e5efc240958:320:29)
at String.Template.manageProject.helpers.email (http://localhost:3000/client/helpers/createProject.js?5f1e645071dc1f451f671ec1d93aa9051ebadc0b:27:29) debug.js:41
不寻常的是,我可以在控制台上运行Meteor.users.findOne(“ ooTw4Ti5juL5vBFXr”)并获得预期的返回值。
相关模板代码:
{{#each projectMembers}}
<li class="userRow">
<div class="lineWrapper">
<div class="destroyUser"></div>
<div class="displayUser">
<div class="userNames">{{email}}</div>
</div>
<div class="projectPermissions">
<div class="admin"><button type="button" class="btn btn-add btn-success btn-sm"><span title="Administrator" class="glyphicon glyphicon-cog {{isAdmin}}"> Admin</span></button></div>
<div class="edit"><button type="button" class="btn btn-add btn-success btn-sm"><span title="Edit" class="glyphicon glyphicon-pencil {{canEdit}}"> Edit</span></button></div>
<div class="download"><button type="button" class="btn btn-add btn-success btn-sm"><span title="Download" class="glyphicon glyphicon-download-alt {{canDownload}}"> Download</span></button></div>
<div class="print"><button type="button" class="btn btn-add btn-success btn-sm"><span title="Print" class="glyphicon glyphicon-print {{canPrint}}"> Print</span></button></div>
<div class="view"><button type="button" class="btn btn-add btn-success btn-sm"><span title="View" class="glyphicon glyphicon-eye-open {{canView}}"> View</span></button></div>
</div>
</div>
</li>
{{/each}}
以及相关的帮助器功能(在控制台中记录错误的位置添加了强调符号(**)):
Template.manageProject.helpers ({
projectMembers: function() {
return this.projectMembers;
},
isPublic: function() {
if (this.publicProject)
{
return "active";
}
else
return "";
},
isNotPublic: function() {
if (this.publicProject)
return "";
else
return "active";
},
debug: function() {
return true;
},
email: function () {
var temp=this;
console.log(temp);
**var userDude=Meteor.users.findOne(temp);**
console.log(userDude);
if (userDude)
{
console.log("found it");
if (!userDude.username)
{
return userDude.emails[0].address;
}
else {
return userDude.username;
}
}
else{
console.log("No dice");
}
return null;
},
enterManageProject: function() {
//assumes called with the session variable currentProject set to the current project id.
//if set to null, create a new project.
var currProject = Session.get("currentProject");
if ((currProject === null) || (currProject === undefined))
{
newProject=Projects.insert(
{
projectName: "New Project",
publicProject: true,
projectMembers: [Meteor.userId()],
projectAdministrators: [Meteor.userId()],
projectEditors: [Meteor.userId()],
projectDownload: [Meteor.userId()],
projectPrint: [Meteor.userId()],
projectView: [Meteor.userId()]
});
currProject=newProject;
Session.set("currentProject",currProject);
}
return Projects.findOne(currProject);
}
})
流星文档显示,将字符串传递给Meteor.users.findOne()应该是合法的,因此,对我做错事的任何见解都将受到赞赏。
提前致谢。
我的用户查找存在类似问题 。 我发现将Meteor.users.findOne调用包装在try..catch块中为我提供了临时解决方案。 就我而言,这似乎稍后会再次被调用。 我仍在努力尝试寻找完整的解决方案,但就我而言,它仅在我未登录时才会显示。
更新:
您可能希望将以下内容作为临时补丁:
try{
var userDude=Meteor.users.findOne(temp);
} catch(e){
console.log(e);
}
更新:
我已经开始阅读有关Meteor的订阅模型的信息。 我发现了一些可能对您有用的方法。 有一种检查帐户服务是否已配置的方法。 我认为这可能对您有用:
if(Accounts.loginServicesConfigured()){
var userDude=Meteor.users.findOne(temp);
}
更新:
按照各方的例子,我已经在服务器上创建了这个:
Meteor.publish("directory", function () {
return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
});
在我的客户中,我正在订阅此服务:
var directorySubscription = Meteor.subscribe("directory");
然后,我正在检查订阅是否准备就绪:
if(directorySubcription.ready()){
var userDude=Meteor.users.findOne(temp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.