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

  • Home
  • SEARCH
  • 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 9244681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:05:44+00:00 2026-06-18T09:05:44+00:00

I have a node.js app that uses mongoose to connect to a mongodb; i

  • 0

I have a node.js app that uses mongoose to connect to
a mongodb; i need to select all the documents inserted and i
i’ve problems with async stuff.

I’ve made a model with the following function:

exports.listItems=function() {
    Ticket.find({}, function(err,tkts) {
            console.log(tkts);
            return tkts;
    });
}

I correctly see the value of “tkts”, but when i call it from:

exports.list = function(req,res) {
    var items=db.listItems();
    console.log("Items:"+items);
    res.render('list', { title: title, items:items });
}

defined in app.js as:

app.get('/list', routes.list);

items is undefined (i think because of non-async definition of db.list()).

What am i doing wrong and how can it be corrected?

  • 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-18T09:05:49+00:00Added an answer on June 18, 2026 at 9:05 am

    You need to use callbacks more appropriately.

    A more traditional listItems function would be

    exports.listItems = function(done) {
        Ticket.find({}, done);
    }
    

    Then, in list, you could do:

    exports.list = function(req,res) {
        db.listItems(function(err,items){
            console.log("Items:"+items);
            res.render('list', { title: title, items:items });
        });
    }
    

    Because of the asynchronous nature of Node.JS, you should always pass (and expect) a callback in your functions. So that you can defer execution if something asynchronous is executed.

    Also: be sure to check out async, its an insanely good and easy-to-use library, that will simplify complex async scenarios in a breeze.

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

Sidebar

Related Questions

We have a node.js app that uses node_msyql , a great little library for
I have a node.js + express.js app that uses jade.js as the templating language.
I have a Rails app that uses MySQL, MongoDB, NodeJS (and SocketIO). Right now,
I have a node.js chat app that uses socket.io and other dependencies but I
I have a module I created for a node.js app. The app also uses
I have a node app that has a string of require s, like this:
I have a simple node app that I am trying to host on cloudfoundry.
I have a node app that I just started working with and each time
I hope you are all well! A question for an app that uses Backbone.js
I have a Rails app that sends requests to a Node.js app. The node.js

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.