0

I'm trying to write my own module for node.js but when i require it, it's empty and the methods I defined are undefined.

// module
var User = require('../models/user');

var UMa = function() {};

UMa.prototype.getUsers = function() {
    User.find({}, function(err, users) {
        return users;
    });
};

module.exports = new UMa();

The first console output is {}, the second is undefined

/*
 * Router for /
 */
var express = require('express');
var userManagement = require('../modules/user-management');
var User = require('../models/user');

var router = express.Router();

router.get('/users', function(req, res) {
    console.dir(userManagement);
    console.log(userManagement.getUsers());
    res.render('users', {users: userManagement.getUsers() });
});
Ahsous
  • 125
  • 2
  • 9
  • You cannot synchronously return an asynchronous value from your `getUsers()` function. This question is probably asked multiple times a day. I will see if I can find a good dup to mark it. `getIsers()` has an async response. You can only return the value via a callback. – jfriend00 Sep 18 '15 at 16:36
  • @Amit - `User.find()` looks exactly like a database call. And the node.js route for `/users` would be an API call. Either is going to be a network operation and going to be async. And, why would in interface return the result via a callback if not async? – jfriend00 Sep 18 '15 at 16:42
  • @Amit - your "answer" does not provide an answer. You don't show ANY solution. You briefly indicate what may be causing the problem, but do not provide a solution. That's not a good answer. Also, with 11k of rep, you should know that this type of question is asked multiple times a day so there is no reason to pollute the repository here by answering yet another duplicate. Do a little research to find a good duplicate and mark it as such. – jfriend00 Sep 18 '15 at 16:47
  • @Amit - You don't answer the actual problem the OP has. If you want to stand by answering the literal question that doesn't actually solve the problem the OP has that's fine, but it's not a very useful answer in the grander scheme of things and thus you should not be surprised if it gets a downvote occasionally. Downvotes and Upvotes are merely votes on how one perceives the quality of the answer and are useful to the community at large as a measure of the perception of the answer. Don't take it so personally OR fix or delete your answer. – jfriend00 Sep 18 '15 at 16:53
  • Why the link to duplicate is a totally difference thing? – praHoc Apr 23 '17 at 14:29

1 Answers1

1

I don't see anything wrong here.

Your getUsers function isn't showed in your first console.log because it's set on the prototype of your userManagement.

The second console.log returns undefined because you're not returning anything in your getUsers function.

You'll have to pass a callback to get your result asynchronously.

// module
UMa.prototype.getUsers = function(cb) {
    User.find(cb);
};

// router
userManagement.getUsers(function (err, users) {
    // Handle result.
});
Yoann
  • 3,020
  • 1
  • 24
  • 34
  • Why does your answer start with "I don't see anything wrong here." when there is clearly something wrong with the code? – jfriend00 Sep 18 '15 at 16:49
  • 1
    His code is not wrong per se... his expectations are. – Yoann Sep 18 '15 at 16:51
  • That's kind of silly. His code does not do what he wants it to do, thus the code is wrong. So, if you wrote this code and checked it in at work and you were called out for writing wrong code, you would argue that the code was correct? Geez. The code is wrong because it does not accomplish what the OP intends. It doesn't make a syntaxError, but it does not accomplish the objective. The code is wrong. – jfriend00 Sep 18 '15 at 16:55
  • I understand now that User.find() doesn't return anything because it's asynchronus. I tried to use a callback like you posted and put a console output inside the callback function and one in getUsers() but none of them gets outputed and my site doesn't load anymore. – Ahsous Sep 18 '15 at 17:31