I want to use ldapjs to search a Microsoft Active Directory (AD) server.
This appears to be a three step procedure. First you need to bind to the server (authenticate), then you need to search & filter the information you are looking for, and then you need to get the results out of the returned Event Emitter.
The SO answer here is very informative, and I have been able to get the desired results server side, but I want to return the searchEntry to the client instead of printing to console. How can I do that?
As I understand the problem: I want to use the Meteor.wrapAsync method server side in order to turn the asynchronous LDAP response from ldapjs synchronous. Meteor.wrapAsync returns a promise and returning that in a Meteor method will resolve that promise and send the result to the client via DPP. Unfortunately, the result contains a circular object that cannot be serialised. So I need to somehow resolve the promise server side and deal with the circular object before returning to the client.
maybe as ldapjs response is synchronous so it already return a promise and you just have to add an await before ? otherwise i’m not experienced with wrapAsync, i’m just surprised that seems an heavy solution, btw you could also read that https://blog.meteor.com/using-promises-and-async-await-in-meteor-8f6f4a04f998 :
we’ll sometimes hit the dreaded Meteor code must always run within a Fiber issue. I’m going to show you how to handle that without resorting to Meteor.wrapAsync
ldapjs response is asynchronous. And maybe I am overcomplicating stuff by using Meteor.wrapAsync, but that was proposed by @robfallows - who also authored that article.
Meteor.wrapAsync does not return a Promise. It just ensures that the wrapped code runs in a Fiber. It also expects the wrapped code to have a callback with the “standard” (error, result) signature. The (asynchronous) wrapped code may then be treated like any other Meteor sync-style code,
If you are seeing Promise-related errors, then as @fg1 suggests, maybe the ldapjs library automatically returns a Promise if a callback is not used (I’ve not used it, so can’t be certain). In that situation, use async/await. So, in a Meteor method:
The callback signature of search.on is not (error, result), so Meteor.wrapAsync will not work as expected. Wrapping the callback in Meteor.bindEnvironment is the way to resolve that. However, it looks like something in there is using Promises, or you would not be getting an UnhandledPromiseRejectionWarning exception,
It uses ldapjs as well, but is a little more convenient to use. It is still required to use all the usual ‘await/async’, ‘futures’, ‘bindenvironment’ type tricks tho. I can show example code if needed, but I get that it is an old thread.