简体   繁体   中英

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. I use the {{#each projectMember}} to iterate over the current members to display either their username (if it exists) or their email address. 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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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