7

If i have schema.js in the same folder as index.js doing something like

var schemas = require('./schema');

works fine, but if i put the schema.js in another folder and write

var schemas = require('./folder/schema');

i get an error Cannot find module whats happening?

Edit1 : I got rid of the error by using ..folder/schema instead of single. and the server runs but it still doesn't work properly as I cant use the mongoosedb object returned through module.export from ../model/schema in the index.js file. It says myModel.find is not a function. Whats going on??

controllers/findallusers.js

var myModel = require('../models/schema');

var alluser;

myModel.find({}, function(err, foundData){   
  if(err){
    console.log(err);
    res.status(500).send();
  }
  else{
        alluser = foundData;
      }

    console.log(alluser); <-- this log is defined
});

    console.log(alluser); <-- this log is undefined

    module.exports = alluser; <-- cant export anything
5
  • 1
    Is the folder directory in the same directory as the index.js file? Commented Sep 14, 2016 at 17:22
  • i mean i have a router folder with index.js and a model folder which has schema.js Commented Sep 14, 2016 at 17:26
  • and im exporting the schema object using module.export in schema,js and trying to use it in index.js by var schemas = require('./model/schema'); Commented Sep 14, 2016 at 17:27
  • It appears that .find() is asynchronous. Therefore IT IS IMPOSSIBLE to return it via module.exports. Instead, export a function that accept a callback or return a promise. Commented Sep 15, 2016 at 3:23
  • @slebetman I'm doing what pawan mentioned, exporting a function list : funtion(req, res){ } but getting syntax error at list: function(req, res) { whats going on? Commented Sep 15, 2016 at 4:45

2 Answers 2

4

Resolve path to schema.js correctly

Assuming your project structure like this

Project
 |
 +-- routers
 |  |  
 |  +-- index.js    
 +-- models
 |  |  
 |  +-- schema.js


//in index.js 
var schemas = require('../models/schema');

To solve second error i.e myModel.find not a function use, module.exports instead of using module.export

module.exports = myModel;

Resolution to your 3rd Problem

// controllers/findallusers.js --> (keep name simple  i.e userController.js)
var myModel = require('../models/schema');

module.exports =  {

    /**
     * Get All Users
     */
    list: function(req, res) {
       myModel.find({},function(err, users){
          if(err) {
              return res.status(500).json({message: 'Error getting Users.'});
          }
         return res.json(users);
      });
    },
   /**
    * Keep Adding more functions as you want
   */

   create: function(req,res){
       //functionality to create user
   },

   delete: function(req,res){
      // functionality to delete user
   },

   someDummyName: function(callback) {
       myModel.find({},function(err, users){
          if(err) {
            return callback(err)
          }
         return callback(null,users);
      });
    }       

}

// Solution to your question https://stackoverflow.com/questions/39504219/how-to-use-use-callbacks-or-promises-to-return-a-object-from-an-asynchronous-fun

//index.js call this new method i.e someDummyName as  
router.get('/allusers', function(req, res){       

    userController.someDummyName(function(err,result){
      if(err) {
        //return err
      }
      //process result as per your need
    });

});
Sign up to request clarification or add additional context in comments.

21 Comments

Is this real life? using two dots before /model/schema just solved the issue -_-
Actually it still doesn't work properly.. I cant use the mongoosedb object returned through module.export from ../model/schema in the index.js file. It says myModel.find is not a function. Whats going on??
I have added the code, also using the model/schema code directly in the index.js works just fine
check my edited answer, and if its solved all your issues accept the answer.
Please read my last edit, i used the .exports a while ago (that was silly of me) and im stuck in a new problem. can you tell me why alluser is getting undefined right after the call back function?
|
0

You are exporting a variable myModel from schema.js and importing just schema in index.js

So, try to reference the .find function using myModel.myModel.find() My guess is that the myModel variable is behind your existing myModel variable in index.js

3 Comments

created a package.json file in /models/ folder with what you mentioned. still getting error Cannot find module './models/schema'
Using ..models/schema solved the server issue of module not found error, but now i cant use the database model object which is exported from schema.js in the index.js. whats going on... can you help?
Theres my attempt, havnt used mongoose before but in my initial research of it, i think that my be it

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.