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 7869091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:12:25+00:00 2026-06-03T01:12:25+00:00

I made the function below for getting usernames from ids. It is not working

  • 0

I made the function below for getting usernames from ids. It is not working well.
I can write console.log(result.first_name); within the query function, and the usernames shows up in my terminal, but not the browser. I tried adding “return ‘something’;” at the end of the function, to see if that showed up in the browser – It did. How can I write the function so that the query result is returned?

function (global function in app.js)

function usernameFromId(id, callback){
    db.users.findOne({ _id: ObjectId(id.toString()) }, function(err, result) {
        var first_name = result.first_name;
        console.log(first_name); // names show up in the console…
        callback(first_name);
    });
};

page handler (in app.js)

app.get('/books', function(req, res){
    function timeSince(dato){
        moment.lang('nb');
        return moment(dato).fromNow();
    };
    db.books.find().sort({ added:-1 }, function(err, docs) {
        var books = docs;
        db.activity.find().limit(9).sort({ time:-1 }, function(err, docs) {
            var activity = docs;
            res.render('books', {
                books: books,
                activity: activity,
                timeSince: timeSince,
                usernameFromId: usernameFromId
            })
        });
    });
});

template (books.jade)

- each a in activity
    p=usernameFromId(a.user_id, function(name){return name;})
  • 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-03T01:12:27+00:00Added an answer on June 3, 2026 at 1:12 am

    Okay, I found a solution. Not sure whether it’s any good, but it works. No need for a function.

    page handler (in app.js):

    app.get('/books', function(req, res){
        db.activity.find().limit(9).sort({ time:-1 }, function(err, docs) {
            var activity = docs;
            db.users.find(function(err, docs) {
                var users = docs;
                res.render('books', {
                    page_title:'books',
                    activity: activity,
                    users: users
                })
            });
        });
    });
    

    template (books.jade):

    - each a in activity
        - for u in users
            - if (a.user_id == u._id.toString())
                | #{u.first_name}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the Ajax function like below. It is not working properly. If I
I made this little function from code snippets around the net. It does what
I have this function below. It recieves a string and a key made out
So I've made custom function for a game I'm working on, one being fade
Why can't this Lua function using a self : be marked local without getting:
I made a function which displays date on the webpage,, and i uploaded the
I made this function to verify a user's twitter credentials. Its running on two
I've made some function that generates an email template. Code it generates is pure
Hi have made this function which is made to replicate an error that I
I have made a function to cound the weeks in a year, and that

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.