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
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?
node.js
add a comment |
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?
node.js
I'm not sure, but I think you need to replacedata()
bydb()
. You exported yourdata
variable (which is yourgetConnection
function), but imported it asdb
.
– Seblor
2 days ago
add a comment |
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?
node.js
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?
node.js
node.js
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 replacedata()
bydb()
. You exported yourdata
variable (which is yourgetConnection
function), but imported it asdb
.
– Seblor
2 days ago
add a comment |
I'm not sure, but I think you need to replacedata()
bydb()
. You exported yourdata
variable (which is yourgetConnection
function), but imported it asdb
.
– 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
add a comment |
2 Answers
2
active
oldest
votes
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');
add a comment |
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
.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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');
add a comment |
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');
add a comment |
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');
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');
answered 2 days ago
Bear NithiBear Nithi
2,571523
2,571523
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered 2 days ago
Hans MusgraveHans Musgrave
2,749519
2,749519
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I'm not sure, but I think you need to replace
data()
bydb()
. You exported yourdata
variable (which is yourgetConnection
function), but imported it asdb
.– Seblor
2 days ago