简体   繁体   English

使用字符串参数访问Meteor.users

[英]Meteor.users access with string argument

I am trying to list the members of a project team, which are stored as userIds in a projectMembers array. 我正在尝试列出项目团队的成员,这些成员作为userIds存储在projectMembers数组中。 I use the {{#each projectMember}} to iterate over the current members to display either their username (if it exists) or their email address. 我使用{{#each projectMember}}遍历当前成员以显示其用户名(如果存在)或电子邮件地址。 Unfortunately, I get an error I haven't been able to debug. 不幸的是,我收到一个我无法调试的错误。

The console logs show that the appropriate userid is getting sent as a string element to Meteor.users.findOne(), but there is an error showing on the console. 控制台日志显示适当的用户标识已作为字符串元素发送到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

What is unusual is that I can run Meteor.users.findOne("ooTw4Ti5juL5vBFXr") on the console and get the expected value returned. 不寻常的是,我可以在控制台上运行Meteor.users.findOne(“ ooTw4Ti5juL5vBFXr”)并获得预期的返回值。

Relevant template code: 相关模板代码:

    {{#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}}

and associated helper functions (with emphasis (**) added where it tosses the error logged in the console): 以及相关的帮助器功能(在控制台中记录错误的位置添加了强调符号(**)):

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);
}
})

The meteor documentation shows that passing a string to Meteor.users.findOne() should be legit, so any insight on what I'm doing wrong is much appreciated. 流星文档显示,将字符串传递给Meteor.users.findOne()应该是合法的,因此,对我做错事的任何见解都将受到赞赏。

Thanks in advance. 提前致谢。

I'm having a similar problem with the user lookup. 我的用户查找存在类似问题 I found that wrapping the Meteor.users.findOne call in a try..catch block gives me a temp solution. 我发现将Meteor.users.findOne调用包装在try..catch块中为我提供了临时解决方案。 In my case this seems to get called again later. 就我而言,这似乎稍后会再次被调用。 I'm still digging to try and find a complete solution, but in my case it only manifests when I'm not logged-in. 我仍在努力尝试寻找完整的解决方案,但就我而言,它仅在我未登录时才会显示。

UPDATE: 更新:

You probably want something like this as a temp patch: 您可能希望将以下内容作为临时补丁:

try{
  var userDude=Meteor.users.findOne(temp);
} catch(e){
  console.log(e);
}

UPDATE: 更新:

I've started reading about the subscription model for Meteor. 我已经开始阅读有关Meteor的订阅模型的信息。 I've found a couple of methods that might be of use to you. 我发现了一些可能对您有用的方法。 There is a method to check if the accounts services are configured. 有一种检查帐户服务是否已配置的方法。 I think this might work for you: 我认为这可能对您有用:

if(Accounts.loginServicesConfigured()){
  var userDude=Meteor.users.findOne(temp);
}

UPDATE: 更新:

As per the parties example I've created this on the server: 按照各方的例子,我已经在服务器上创建了这个:

Meteor.publish("directory", function () {
  return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
});

In my client, I'm subscribing to this service: 在我的客户中,我正在订阅此服务:

var directorySubscription = Meteor.subscribe("directory");

Then I'm checking if the subscription is ready: 然后,我正在检查订阅是否准备就绪:

if(directorySubcription.ready()){
  var userDude=Meteor.users.findOne(temp);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM