Skip to content

Instantly share code, notes, and snippets.



Last active Jan 2, 2016
What would you like to do?
get your nodebb users emails in a CSV file to import to mailchimp or something
// !!!!! IMPORTANT !!!!!!
// this is no longer needed since a helper users/csv generator was added by @psychobunny
// commit:
// in order for this to work
// - this JS file needs to be located in: [NodeBB_PATH]/node_modules/_nodebb_get_users_emails
// - you must have a working NodeBB installation, with config.json and a running database
// usage:
// cd [NodeBB_PATH]/node_modules/_nodebb_get_users_emails
// npm install fs-extra
// npm install async
// node nodebb.emails.js
// then find the data in emails.csv
var fs = require('fs-extra'),
async = require('async'),
path = require('path'),
emailsFile = path.join(__dirname, './emails.csv'),
User, db, nconf;
// todo: this is such a bummer !!!
// in order to require almost any NodeBB Object, nconf.get('database') needs to be set
// so let's require nconf first
nconf = require('../nconf');
var nconfFile = path.join(__dirname, '../../config.json');
// see if the NodeBB config.json exists
if (fs.existsSync(nconfFile)) {
// config.json is there, load it and use these values
var nconfigs = fs.readJsonSync(nconfFile);
nconfigs.use_port = nconfigs.use_port ? 'y' : 'n';
} else {
// config.json does not exists, i'm throwing an error
throw new Error('run: node app --setup # to create a config.json');
//tell nconf to read it the config
// i know it's redundant, but im not sure if I can pass the values in memory, but what the hell, it's not a huge file anyways
nconf.file({file: nconfFile});
// requiring DB after configs, since it could be either mongo or redis now
if (nconfigs.database === 'redis') {
nconf.set('database', 'redis');
} else if (nconfigs.database === 'mongo') {
nconf.set('database', 'mongo');
} else {
throw new Error('NodeBB Database config is not set');
// activated or not, still works if it lives in NodeBB/node_modules/_nodebb_get_users_emails
try {
User = module.parent.require('./user.js');
db = module.parent.require('./database.js');
} catch (e) {
User = require('../../src/user.js');
db = require('../../src/database.js');
var generateUsersEmails = function(){
console.log('getting users emails started');
var t0 = +new Date();
var csvContent = 'email,username,uid\n';
db.getObjectValues('username:uid', function(err, uids) {
async.each(uids, function(uid, next) {
console.log('reading user:' + uid);
User.getUserFields(uid, ['email', 'username'], function(err, userData) {
if(err) {
return next(err);
csvContent += ',' + userData.username + ',' + uid +'\n';
}, function(err) {
if (err) throw err;
console.log('reading done, now writing to ' + emailsFile);
fs.writeFile(emailsFile, csvContent, function(err){
if (err) throw err;
console.log('getting users emails took: ' + ((+new Date() - t0) / 1000 / 60).toFixed(2) + ' minutes');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.