Ldapjs search does not appear to end - javascript

I'm new to ldapjs and ldap in general. I have the following code currently:
var search = client.search(base, opts, function(err, res) {
assert.ifError(err);
res.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
});
res.on('searchReference', function(referral) {
console.log('referral: ' + referral.uris.join());
});
res.on('error', function(err) {
console.error('error: ' + err.message);
});
res.on('end', function(result) {
console.log('status: ' + result.status);
});
});
My search occurs correctly and finds everything needed. My issue is that it does successfully get to res.on('end') and prints out the status code (0) but the script I'm running never "officially" ends and thus causes me to have to perform a keyboard interrupt.
Additionally I tried using res.end() but was informed this was not a function. Is there something I am missing entirely? Thanks!

I think it is more nodejs question rather than ldapjs.
You can add exit your program on the 'end' event.
See:
How to exit in Node.js

Try doing this for your res.on('end'...)
res.on('end', (result => {
console.log('status: ' + result.status);
client.destroy();
});

Related

How can I concatenate .load() jquery method?

I am using the cordova plugin phonegap-plugin-portrait-barcodescanner on my PhoneGap App.
So I can't combine the variable result and Scanner result. I have that combine this results on .load(VAR + SCANNER RESULT);
In javascript alert it's ok. But in Load(); not working.
Thankks!
$('#scan').click(function() {
cordova.plugins.barcodeScanner.scan(
function(result) {
var irisUrl = 'http://www.vcomm.com.br/';
$('#Load').load(irisUrl + result.text);
alert("http://www.vcomm.com.br/" + result.text);
},
function(error) {
alert("Scanning failed: " + error);
}
);
});
maybe you have to try somethig like this:
$('#Load').load(irisUrl, function () {
irisUrl = $(this) + result.text();
});
or something like this. I'm not sure because I don't know the result of that alert in your example. What do you want to see etc.

why isn't my casperjs displaying any errors?

var casper = require('casper').create({
logLevel:'deubg',
verbose:true,
});
casper.start(someurl,function(){
not_existing_function();
})
When executing above code, all I see on the screen is debug informations that means very little to me. I am expecting to see some error saying the function being called doesn't exist, but there is not.
I thought it was just the behavior, until I see this.
The question clearly indicates he has got some error message:
ReferenceError: Can't find variable: $
Why can't I see something like this on my screen?
You're likely using PhantomJS 2.x. It has a known bug where some errors are not reported. That includes the class of errors that you're describing.
Also, registering to the various error events of CasperJS/PhantomJS doesn't help in this case, but here they are just in case:
// http://phantomjs.org/api/phantom/handler/on-error.html
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR: ' + msg];
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
console.error(msgStack.join('\n'));
phantom.exit(1);
};
// http://docs.casperjs.org/en/latest/events-filters.html#remote-message
casper.on("remote.message", function(msg) {
this.echo("Console: " + msg);
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-error
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg);
// maybe make it a little fancier with the code from the PhantomJS equivalent
});
// http://docs.casperjs.org/en/latest/events-filters.html#resource-error
casper.on("resource.error", function(resourceError) {
this.echo("ResourceError: " + JSON.stringify(resourceError, undefined, 4));
});
// http://docs.casperjs.org/en/latest/events-filters.html#page-initialized
casper.on("page.initialized", function(page) {
// CasperJS doesn't provide `onResourceTimeout`, so it must be set through
// the PhantomJS means. This is only possible when the page is initialized
page.onResourceTimeout = function(request) {
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request));
};
});
You can run something like eslint or jshint over the script to catch syntax errors and you can run your script in PhantomJS 1.9.8/1.9.7 in order to catch these sort of errors.

Casperjs catch console.log and console.error

I'am trying to catch site console.log and console.error by casperjs. In the case of console.log I have working code:
casper.on('remote.message', function(message) {
this.echo('remote message caught: ' + message);
});
But I can't figure out how to catch console.error. I need this for catching any resources error (like images not found).
There's also the page.error handler:
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
});
Depending on which errors you need to catch, this one may be better.
Ok it's weird to answer my own question but I found a solution on a coderwall blog posted by dpashkevich:
casper.on('resource.received', function(resource) {
var status = resource.status;
if(status >= 400) {
casper.log('Resource ' + resource.url + ' failed to load (' + status + ')', 'error');
resourceErrors.push({
url: resource.url,
status: resource.status
});
}
});
Works brilliant
You can use the following event to get remote errors:
casper.on("resource.error", function(resourceError) {
this.echo("Resource error: " + "Error code: "+resourceError.errorCode+" ErrorString: "+resourceError.errorString+" url: "+resourceError.url+" id: "+resourceError.id, "ERROR");
});
Works like charm!

How to get the output from NodeJS when use the child_process.spawn to launch a bat on windows OS, and the bat will open a new terminal

If the bat only run one terminal ,we can got the stdout, but we will failded if this open a new window.
var terminal = require('child_process').spawn('aa.bat');
console.log('Starting..terminal.pid.', terminal.pid, "process.pid", process.pid);
terminal.stdout.on('data', function(data) {
console.log('stdout:',data);
});
terminal.stderr.on('data', function(data) {
console.log('stderr:',data);
});
terminal.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
terminal.on('exit', function(code) {
console.log('exit code:', code, ' terinal.pid.', terminal.pid, "process.pid", process.pid);
console.log('child process', process.pid, 'exited with code ' + code);
});
presumed the bat file like this
start cmd
if we change it to
start /b cmd
this will not open a new terminal ,the nodeJs will work
It's difficult to get another process stdout from NodeJS on windows os, at most time it open an new terminal window. but you can use an bash to launch it.
1.The bash.exe you can download from http://www.steve.org.uk/Software/bash/
then the code maybe like this:
var terminal = require('child_process');
function start() {
if (process.platform.trim() !== 'win32') {
terminal = terminal.spawn('bash');
console.log('This is not the win32 plantform ,please confirm the bash woking!');
} else {
terminal = terminal.spawn('./bash-2.03/bash.exe');
}
// !!!must append the ./
//terminal.stdin.write('./aa.exe');
terminal.stdin.write('./aa.bat');
terminal.stdin.end();
};
start();
terminal.stdout.on('data', function(data) {
console.log(data + "");
});
terminal.stdout.on('error', function(data) {
console.log('error:\n' + data);
});
terminal.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
terminal.on('exit', function(code) {
console.log('exit code:', code, ' terinal.pid.', terminal.pid, "process.pid", process.pid);
console.log('child process', process.pid, 'exited with code ' + code);
});

Node.js socket hang up error when connecting to twitter api

I'm getting socket hang up error , when connecting to twitter streaming api with basic authentication using https. I think the error is occurring when doing the authentication.
var https = require("https");
var options = {
host: 'stream.twitter.com',
path: '/1.1/statuses/filter.json?track=bieber',
method: 'GET',
headers: {
"Authorization": "Basic " + new Buffer('UserName' + ":" + 'Password').toString("base64")
}
}; //end of options
var request = https.request(options, function(response){
var body = '';
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.on("data", function(chunk){
var tweet = JSON.parse(chunk);
console.log("Tweet " + tweet.text);
}); // end of data
response.on("end", function(){
console.log("Disconnected!!");
}); // end of end
response.on("error", function(){
console.log("error occured");
}); // end of error
}); // end of request
request.on("error",function(error){
console.log("Error: " + error.message);
}); // end of error
When i try to access this url it works fine.
https://username:password#stream.twitter.com/1.1/statuses/filter.json?track=twitter
Change https.request(options, function(response){ } to https.get(options, function(response){ } Thanks to Darrenlooby on nodejs IRC for pointing it out.
You can use https.request, but make sure you add:
request.end();
where request is the object returned from https.request
See http://nodejs.org/api/https.html#https_https_request_options_callback for an example.
I had the same issue and this fixes it :)

Categories

Resources