0

I don't know if it is possible or maybe I don't know how to quite google the question properly but I am wondering is their a way to query the MongoDB from one file and return the results to another using node. Lets say I have two files routes.js and helpers.js

routes.js

const finder = require('../db_helpers/userHelpers');

exports.getIndex = (req, res, next) => {
        finder.findUser()
            .then(user => {
                if (!user) {
                    return res.redirect('/signup')
                }
                res.render('shop/landing', {
                    pageTitle: 'landing'
                });
            })
            .catch(err => {
                console.log(err)
            })
    };

helpers.js

const User = require('../models/user');

exports.findUser = (user) => {
    User.findOne()
        .then(user => {
            console.log(user);
            return user
        })
        .catch(err => {
            return err
        })
};

This is what I have been working with for a few hrs now changing things around and such but to no avail. Like I said I may have been googling wrong but if someone could point me in the right direction or tell me this isn't possible that would be greatly appreciated.

Mani
  • 1,471
  • 1
  • 13
  • 19
Perry Craft
  • 309
  • 3
  • 15
  • Are you passing data to `finder.findUser()` and `User.findOne()`? – Anurag Srivastava Mar 04 '19 at 21:29
  • So what is the problem? What errors have you seen whilst working on this for hours? You are clearly working off a pattern of code you have seen before so, Possible? YES. What your current problem is? We don't know because you did not tell us. If you see errors, then show errors, since those we can actually give some input on. – Neil Lunn Mar 04 '19 at 21:32
  • Oh and the code correction. `exports.findUser = (user) => { return User.findOne()` or just `exports.findUser = (user) => User.findOne()`. You are missing the `return` or what is otherwise an *implicit `return`* when you omit the "block" `{}`. Shout have just said you had an "undefined property .then" error, or in fact just "google" that error string. – Neil Lunn Mar 04 '19 at 21:35
  • Duplicate of : [TypeError: Cannot read property 'then' of undefined](https://stackoverflow.com/questions/24788171/typeerror-cannot-read-property-then-of-undefined) – Neil Lunn Mar 04 '19 at 21:36
  • I was passing in user on a few trials but when i did I always got a reference error saying user is not defined. @AnuragSrivastava – Perry Craft Mar 04 '19 at 21:44

1 Answers1

0

The problem is that you are expecting a promise with finder.findUser().then() in the routes.js file but not returning a promise in userHelpers.js, so the then statement never invoked since the promise is never met.

Your userHelpers.js file should look like:

const User = require('../models/user');

exports.findUser = user => {
  User.findOne((err, user) => {
    if (err) return Promise.reject(err);

    return Promise.resolve(user);
  });
};
Borduhh
  • 1,975
  • 2
  • 19
  • 33