FindOne with MongooseJS and then storing the result in advance?

Sorry if this sounds silly, I am still learning server-side Javascript. I have this code in my app.js file.

var test;

UserModel.findOne({name: 'guest'}, function(err, user) {
    test = user.name;
});

console.log(test);

I understand that this doesn't work because UserModel.findOne is async and so everything has to happen within the callback. E.g. console.log(test) could execute before the UserModel.findOne finishes.

Question

If I declare var test in my app.js file, is there a way of getting the value of user.name (via UserModel.findOne) into it?

The reason why I am doing this is because I am hoping to retrieve all these information from the database in advance. So that I don't have to make a database query every time need a user's name.

If this idea is unsound, are there similar alternatives?

Edit/Update

This isn't used for authentication/log-in. I intend for all users to have access to the variable test and its content.

Essentially, I am asking why doesn't the variable test set permanently to user.name but instead only got step within the scope of the callback. And, is it possible to set the variable permanently?

The example:

var users = {};
function getUser(username, callback) {
    if (typeof users[username] !== 'undefined') {
        callback(users[username]);
    } else {
        Users.findOne({"name":username}, function(err, user) {
            users[username] = user;
            callback(user);
        });
    };
};

This will always return the named user either from the cache or from mongodb. It is called the same way as the findOne:

getUser('guest', function(user) { console.log(user); });

After that you could access a user from the cache directly or assign it to a variable. But you must ensure that getUser has succeeded first.

One of the hardest things about Node.js is to understand that code is called outside of the normal flow of the written source. It is not enough for the code to follow the call to a function with a callback.

Considering that all users of your app would share that test variable, this seems like a bad idea. This is the sort of data you would store in the user's session if you don't want to look it up as needed.

EDIT

As long as you're declaring test at either global or module scope, it is set permanently in the findOne callback.

Yes you can apply the result to a variable in a higher scope for example this should work;

var test;

function getTest() {
   console.log(test);
}

UserModel.findOne({name: 'guest'}, function(err, user) {
   test = user.name;

   getTest();
});

Check out the answer by hexacyanide below. Using a callback in the async callback saved me for my project with this same problem.

Set Variable to result of Mongoose Find