using a function which was imported from another module(node.js)2019 Community Moderator ElectionIn Node.js, how do I “include” functions from my other files?How do I resolve “Cannot find module” error using Node.js?How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)How to show data from mysql in nodejs with a refresh rateUsing Node.js require vs. ES6 import/exportWhy this error coming while running nodejs server?Express req.body not WorkingManaging database connections in Node.js, best practices?I got an empty array in sub document array saving using mongoose ( MEAN stack)Why express.Router() while separating routes

A running toilet that stops itself

Paper published similar to PhD thesis

I am the person who abides by rules but breaks the rules . Who am I

ESPP--any reason not to go all in?

Giving a talk in my old university, how prominently should I tell students my salary?

Why restrict private health insurance?

I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?

What is 'Log Memory' in Query Store 2017

What is Tony Stark injecting into himself in Iron Man 3?

Vector-transposing function

Will the concrete slab in a partially heated shed conduct a lot of heat to the unconditioned area?

How does a sound wave propagate?

A vote on the Brexit backstop

Is it appropriate to ask a former professor to order a library book for me through ILL?

What exactly is the meaning of "fine wine"?

What is better: yes / no radio, or simple checkbox?

Is there a logarithm base for which the logarithm becomes an identity function?

Exempt portion of equation line from aligning?

Create chunks from an array

Do I need a return ticket to Canada if I'm a Japanese National?

Did Amazon pay $0 in taxes last year?

Is there a math expression equivalent to the conditional ternary operator?

Are small insurances worth it?

Ultrafilters as a double dual



using a function which was imported from another module(node.js)



2019 Community Moderator ElectionIn Node.js, how do I “include” functions from my other files?How do I resolve “Cannot find module” error using Node.js?How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)How to show data from mysql in nodejs with a refresh rateUsing Node.js require vs. ES6 import/exportWhy this error coming while running nodejs server?Express req.body not WorkingManaging database connections in Node.js, best practices?I got an empty array in sub document array saving using mongoose ( MEAN stack)Why express.Router() while separating routes










0















that is my db.js from where i am exporting the function



 var mysql=require('mysql');

var config=

host:'127.0.0.1',
user:'root',
password:'',
database:'travel'

;
var con="";
var data=function getConnection()

con=mysql.createConnection(config);
con.connect(function(err)

if(err!=null)

console.log('connection id: '+con.threadId);

else

console.log('connection error: '+err.stack);


)
;
module.exports=data;


and here is my login.js file from where i am calling that exported data function



var express=require('express');
var db = require.main.require('./model/db');
var router=express.Router();

//routes
router.post('/',function(req,res)

console.log(req.body);

);

router.get('/',function(req,res)

data();
res.render('login');
);

module.exports = router;


Here is the error message after running the code. can anyone please help me with that problem?



error message










share|improve this question
























  • I'm not sure, but I think you need to replace data() by db(). You exported your data variable (which is your getConnection function), but imported it as db.

    – Seblor
    2 days ago















0















that is my db.js from where i am exporting the function



 var mysql=require('mysql');

var config=

host:'127.0.0.1',
user:'root',
password:'',
database:'travel'

;
var con="";
var data=function getConnection()

con=mysql.createConnection(config);
con.connect(function(err)

if(err!=null)

console.log('connection id: '+con.threadId);

else

console.log('connection error: '+err.stack);


)
;
module.exports=data;


and here is my login.js file from where i am calling that exported data function



var express=require('express');
var db = require.main.require('./model/db');
var router=express.Router();

//routes
router.post('/',function(req,res)

console.log(req.body);

);

router.get('/',function(req,res)

data();
res.render('login');
);

module.exports = router;


Here is the error message after running the code. can anyone please help me with that problem?



error message










share|improve this question
























  • I'm not sure, but I think you need to replace data() by db(). You exported your data variable (which is your getConnection function), but imported it as db.

    – Seblor
    2 days ago













0












0








0








that is my db.js from where i am exporting the function



 var mysql=require('mysql');

var config=

host:'127.0.0.1',
user:'root',
password:'',
database:'travel'

;
var con="";
var data=function getConnection()

con=mysql.createConnection(config);
con.connect(function(err)

if(err!=null)

console.log('connection id: '+con.threadId);

else

console.log('connection error: '+err.stack);


)
;
module.exports=data;


and here is my login.js file from where i am calling that exported data function



var express=require('express');
var db = require.main.require('./model/db');
var router=express.Router();

//routes
router.post('/',function(req,res)

console.log(req.body);

);

router.get('/',function(req,res)

data();
res.render('login');
);

module.exports = router;


Here is the error message after running the code. can anyone please help me with that problem?



error message










share|improve this question
















that is my db.js from where i am exporting the function



 var mysql=require('mysql');

var config=

host:'127.0.0.1',
user:'root',
password:'',
database:'travel'

;
var con="";
var data=function getConnection()

con=mysql.createConnection(config);
con.connect(function(err)

if(err!=null)

console.log('connection id: '+con.threadId);

else

console.log('connection error: '+err.stack);


)
;
module.exports=data;


and here is my login.js file from where i am calling that exported data function



var express=require('express');
var db = require.main.require('./model/db');
var router=express.Router();

//routes
router.post('/',function(req,res)

console.log(req.body);

);

router.get('/',function(req,res)

data();
res.render('login');
);

module.exports = router;


Here is the error message after running the code. can anyone please help me with that problem?



error message







node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Seblor

2,550925




2,550925










asked 2 days ago









EaJaJuL RaTaNEaJaJuL RaTaN

61




61












  • I'm not sure, but I think you need to replace data() by db(). You exported your data variable (which is your getConnection function), but imported it as db.

    – Seblor
    2 days ago

















  • I'm not sure, but I think you need to replace data() by db(). You exported your data variable (which is your getConnection function), but imported it as db.

    – Seblor
    2 days ago
















I'm not sure, but I think you need to replace data() by db(). You exported your data variable (which is your getConnection function), but imported it as db.

– Seblor
2 days ago





I'm not sure, but I think you need to replace data() by db(). You exported your data variable (which is your getConnection function), but imported it as db.

– Seblor
2 days ago












2 Answers
2






active

oldest

votes


















0














You should change the import statement as follows, because you are invoking the data() not db(). so you should change the import statement to data instead of db



var data = require.main.require('./model/db');





share|improve this answer






























    0














    When you use module.exports you're specifying the object that is exported, not a name or set of names. Since you literally just export the data function you want to use, the line var db = require.main.require('./model/db'); first creates/finds that function and then assigns it to the variable db -- the original name doesn't matter at all.



    Possible solutions to that problem (as indicated in the other answers) include replacing data() with db() or replacing var db with var data.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023504%2fusing-a-function-which-was-imported-from-another-modulenode-js%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You should change the import statement as follows, because you are invoking the data() not db(). so you should change the import statement to data instead of db



      var data = require.main.require('./model/db');





      share|improve this answer



























        0














        You should change the import statement as follows, because you are invoking the data() not db(). so you should change the import statement to data instead of db



        var data = require.main.require('./model/db');





        share|improve this answer

























          0












          0








          0







          You should change the import statement as follows, because you are invoking the data() not db(). so you should change the import statement to data instead of db



          var data = require.main.require('./model/db');





          share|improve this answer













          You should change the import statement as follows, because you are invoking the data() not db(). so you should change the import statement to data instead of db



          var data = require.main.require('./model/db');






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Bear NithiBear Nithi

          2,571523




          2,571523























              0














              When you use module.exports you're specifying the object that is exported, not a name or set of names. Since you literally just export the data function you want to use, the line var db = require.main.require('./model/db'); first creates/finds that function and then assigns it to the variable db -- the original name doesn't matter at all.



              Possible solutions to that problem (as indicated in the other answers) include replacing data() with db() or replacing var db with var data.






              share|improve this answer



























                0














                When you use module.exports you're specifying the object that is exported, not a name or set of names. Since you literally just export the data function you want to use, the line var db = require.main.require('./model/db'); first creates/finds that function and then assigns it to the variable db -- the original name doesn't matter at all.



                Possible solutions to that problem (as indicated in the other answers) include replacing data() with db() or replacing var db with var data.






                share|improve this answer

























                  0












                  0








                  0







                  When you use module.exports you're specifying the object that is exported, not a name or set of names. Since you literally just export the data function you want to use, the line var db = require.main.require('./model/db'); first creates/finds that function and then assigns it to the variable db -- the original name doesn't matter at all.



                  Possible solutions to that problem (as indicated in the other answers) include replacing data() with db() or replacing var db with var data.






                  share|improve this answer













                  When you use module.exports you're specifying the object that is exported, not a name or set of names. Since you literally just export the data function you want to use, the line var db = require.main.require('./model/db'); first creates/finds that function and then assigns it to the variable db -- the original name doesn't matter at all.



                  Possible solutions to that problem (as indicated in the other answers) include replacing data() with db() or replacing var db with var data.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Hans MusgraveHans Musgrave

                  2,749519




                  2,749519



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023504%2fusing-a-function-which-was-imported-from-another-modulenode-js%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                      Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                      Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved