Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:45:29+00:00 2026-06-11T16:45:29+00:00

Sorry if this sounds silly, I am still learning server-side Javascript. I have this

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:45:30+00:00Added an answer on June 11, 2026 at 4:45 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this question sounds a little silly, but I am not sure what
Sorry if this sounds like a really silly question. But I Googled the web
Sorry if this sounds like a bit of a noobie question, i'm still relatively
Sorry if this sounds childish. I have been trying to manipulate a canvas element
Sorry if this sounds dumb, excuse this newbie. Here is my code line=abcd<>efgh->higk<-1234 v1=$(echo
sorry if this question sounds useless . I am researching about google app engine
Sorry, this questions sounds silly, but after developing some of my RESTful services using
This is going to sound too silly / too basic - sorry about that,
Sorry if the title sounds confusing - but this is what I am trying
Sorry if this sounds obvious but i'm getting confused. I'm trying to build a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.