With Node I am trying to collect user data from an LDAP server and then write that data to a JSON file. I am using the following code to do this:
fs.writeFile('data.json', JSON.stringify(data, null, 4));
The problem is the JSON.stringify
method is causing the following error:
FATAL ERROR: JS Allocation failed - process out of memory
I know the problem is with JSON.stringify
because if I use console.log
rather than fs.writeFile
I get the same error.
I am trying to write a lot of data (over 500 entries in the LDAP database). Does anyone know how I can get this to work? Here is the code in full:
var ldap = require('ldapjs');
var util = require('util');
var fs = require('fs');
var client = ldap.createClient({
url: '************'
});
client.bind('CN=**********,OU=Users,OU=LBi UK,OU=UK,DC=********,DC=local', '*********', function(err) {
if (err) {
console.log(err.name);
}
});
// taken from http://ldapjs.org/client.html
client.search('OU=Users,OU=******,OU=UK,DC=******,DC=local', {
scope: 'sub',
filter: 'objectClass=organizationalPerson',
attributes: ['givenName', 'dn', 'sn', 'title', 'department', 'thumbnailPhoto', 'manager']
// filter by organizational person
}, function(err, res) {
if (err) {
console.log(err.name);
}
var limit = 1;
var data = {"directory": []};
res.on('searchEntry', function(entry) {
var obj = {};
entry.attributes.forEach(function (attribute) {
var value;
if (attribute.type === 'thumbnailPhoto') {
value = attribute.buffers[0];
} else {
value = attribute.vals[0];
}
obj[attribute.type] = value;
});
data.directory.push(obj);
});
res.on('error', function(err) {
console.log('error: ' + err.message);
});
res.on('end', function(result) {
fs.writeFile('data.json', JSON.stringify(data, null, 4));
});
});
Something is happening recursively.
Make sure that your Object
data
does not contain any circular references, such as tothis
or anything else that has difficulty being serialized.As @freakish mentioned the problem was my data was too big.
The reason the data was so big was due to a large number of images that were being returned as objects. In the end all I needed to do was encode the object as base64 using Buffers and then the size of the data became much more manageable.