Node CSV Parser output issue

I am trying to use Node CSV Parser to read in a query result from my Mongo database and write it out to a csv file.

When I run my script, I get a csv file with two [object] entries on the first line and everything else blank.

This is my code:

// node samples/sample.js
var csv = require('csv');

var mongoose = require('mongoose');

var User = require('../models/userModel').User;

dsn = "mongodb://localhost/test";

mongoose.connect(dsn);

console.log ("here");

var users = User.find({}, function(err, result){
        console.log(result);
        });
var columns = ['userName','agencyName','email','password','id']; 

csv()
.from(users, {
    columns: true
    })
.toPath(__dirname+'/users.csv',{
    columns: ['userName','agencyName','email','password','id']
    })
.transform(function(data){
    data.unshift(data.pop());
    return data;
})
.on('data',function(data,index){
    console.log('#'+index+' '+JSON.stringify(data));
})
.on('end',function(count){
    console.log('Number of lines: '+count);  
})
.on('error',function(error){
    console.log(error.message);
});

This is the data displayed in terminal:

[ { userName: 'LasVegas',
    agencyName: 'City of Las Vegas',
    email: 'lasvegas@gmail.com',
    password: 'upload1',
    _id: 4ffe2e6bddc0c02b15000001 } ]

I have only entered one user into the database so I believe it is correct.

Any help would be greatly appreciated!

EDIT:

Here is my code after trying to call csv in the callback:

// node samples/sample.js
var csv = require('csv');

var mongoose = require('mongoose');

var User = require('../models/userModel').User;

dsn = "mongodb://localhost/test";

mongoose.connect(dsn);

console.log ("here");

var columns = ['userName','agencyName','email','password','id']; 

var users = User.find({}, function(err, result){
    console.log(result);

if (err){
    callback(err,null);
}else{

csv()
.from(users, {
columns: true
})
.toPath(__dirname+'/users.csv',{
columns: ['userName','agencyName','email','password','id']
})
.transform(function(data){
data.unshift(data.pop());
return data; 
})
.on('data',function(data,index){
console.log('#'+index+' '+JSON.stringify(data));
})
.on('end',function(count){
console.log('Number of lines: '+count);
})
.on('error',function(error){
console.log(error.message);
})
}
});

Now instead of two [object] fields, the fields contain weird symbols and I am asked to choose a filter when opening the file.

Your call to User.find() is asynchronous; it's not assigning the results of the search to users. You need to move your call to csv into the callback and either replace the reference to users there with results or rename the callback parameter to users.