I do not believe that is correct. Every method on the server has the userId in the this context.
For example the following method allows to retrieve the username for any entry in Meteor.users if the requesting user has role admin
Meteor.methods({
user_name: function(id) {
if(this.isSimulation) {
// stub method runs on client and message will be in the browser console
console.log('user_name for '+id+' called by '+this.userId);
} else {
// the following will be in the server console
console.log('user_name for '+id+' called by '+this.userId);
if (Roles.userIsInRole(this.userId, ['admin'])) {
return Meteor.users.findOne({ _id: id }).username;
} else {
throw new Meteor.Error(403, 'Access Denied', 'You must be member of the group admin');
}
}
}
});
Meteor.call('user_name', <some id>, function(err, user_name) {
if(err) console.error(err);
else console.log(user_name);
});
The question is if your Method runs on the client (the stub) and your collection doesn’t contain the requested record.
Yeah sorry, I checked out the guide for validated methods and they are doing the same thing.
@zxcvbnm1234567890 Sorry, I can’t tell from your code what the problem is. If you could create a small repro isolating the problem I could take a shot at debugging it.
Be careful when you fetch data in methods, if the method is shared by client and server.
On the client Candidatos.findOne({propietario: this.userId}); may return undefined, because the data might not be published there. On the server this should always return a result (if the document exists, of course).
So check what’s happening on the server and ignore the client. Maybe even wrap the content of run inside a if(Meteor.isServer)
Edit: As a rule of thumb, i would say: if you use find or findOne in a method, declare this part only on the server. If you need latency compensation, try to write a client-stub, that works without this related data (from find).
If you declare methods on client and server, the client does a simulation for latency compensation and the server will compute the real value. If the server will have a different result, the client will revert the simulation and update everything with the server-value.
This works properly, when you do just an insert. It mostly works, when you fetch other data in your method, as long as this data is published. In the Todo- example that you linked, it fetches the List-document from the given listId. So it’s likely that this document is already published.
But if you do more complex logic with more data in your method, you risk that the client-stub will often return a different result than the server due to missing data and therefore latency compensation no longer works. If the client-simulation will throw an error because of missing data, the method will not be called on the server as far as i remember.
To come back to your initial question: what is logged by the client and by the server in console.log('candidato ', candidato); ? If candidato is undefined on the client, the next line will crash (Because of candidato._id). If so, you should either guarantee that this data is published (hard), do the logic only on the server (wrap it in if (Meteor.isServer), or catch the case where candidat is undefined on the client (you could therefore just ommit candidatoId in the document until the server reports the correct result)