I’m trying to wrap an asynchronous function and call it synchronously using Meteor.wrapAsync(). I’m trying to move from the below server-side asynchronous call (which works fine):
if (!err) {
Meteor.call("ldapLogin", username, password, Meteor.user());
}
where ldapLogin is in another server-side js file:
You have a few typos in your sample. The method you call should have a matching method name defined, and since you’re passing in parameters you need to receive them. For example if you have:
Meteor.call('ldapLogin', username, password);
you would then have something like the following defined:
Meteor.methods({
'ldapLogin': function (username, password) {
check(username, String);
check(password, String);
// Assuming "processLdapLogin" exists somewhere else ...
var syncFunc = Meteor.wrapAsync(processLdapLogin);
var result = syncFunc(username, password);
console.log(result);
}
});
I do have a processLdapLogin method in another js file which was being called fine using the async Meteor.call. After making your suggested changes, processLdapLogin is not being called when I try to step through. This is what my code looks like now:
if (!err) {
Meteor.call('ldapLogin', username, password);
Meteor.methods({
'ldapLogin': function(username, password) {
check(username, String);
check(password, String);
var syncFunc = Meteor.wrapAsync(processLdapLogin);
var result = syncFunc(username, password, Meteor.user());
console.log(result);
}
});
}
My bad, I’ve moved the calling statement. The code block above is within a server-side method called from the client on a sign-in event (login form submit). The processLdapLogin method is also on the server, in another js file. After moving the calling statement, I encounter the following error:
Are you sure the method is only on the server? The reference to .../lib/... in the exception suggests that your client has the method defined as well - in which case, it’s trying to run it as a simulation (and failing because processLdapLogin is not available on the client). As a workaround, you could just wrap the offending code in a if (Meteor.isServer) block. Better, you should recheck the structure of your code to ensure the method is defined only on the server (in a server/ folder).
When you say “the method”, are you referring to processLdapLogin? This is in fact a server-only method in server/ldap.js. When I only had the asynchronous method call, it would actually call the following Meteor Method in server/ldap.js:
However, I’ve since commented out the above in server/ldap.js and call processLdapLogin in server/ldap.js directly using Meteor.wrapAsync(processLdapLogin). Is this incorrect?