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.

5
  • Are you passing data to finder.findUser() and User.findOne()? Commented Mar 4, 2019 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. Commented Mar 4, 2019 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. Commented Mar 4, 2019 at 21:35
  • Duplicate of : TypeError: Cannot read property 'then' of undefined Commented Mar 4, 2019 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 Commented Mar 4, 2019 at 21:44

1 Answer 1

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);
  });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.