Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:07:28+00:00 2026-06-04T18:07:28+00:00

I have config.js in my nodejs project var mysql = require(‘mysql’); var db_name =

  • 0

I have config.js in my nodejs project

var mysql  = require('mysql');
var db_name = 'node';
var client = mysql.createClient({
  user: 'root',
  password: 'pass',
});
client.query('USE '+db_name);

I have used it as config file for the project. How I can use this code to call a mysql query.

I have this code in user.js

var global = require('./config.js');

this.CheckUserValid = function (login, pass) {
    var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
    global.client.query(sql, [login, pass], function selectResutl(err, results, fields) {
        if (!err) return results;
        else
            throw err;
    });
}

TypeError: Cannot call method 'query' of undefined
    at Object.CheckUserValid (C:\wamp\www\Node\user.js:6:19)

Can someone help me to let me know how I can do this in better way.Actually I am using asp.net mvc in my past so I have tried it. Do someone tell me the better way to write code in nodejs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:07:30+00:00Added an answer on June 4, 2026 at 6:07 pm

    When you require() a file/module, the result you get is whatever the module is exporting through the exports property. Anything else is encapsulated in the module and cannot be accessed from the outside. In your case, at the end of config.js you can add:

    exports.client = client;
    

    This way it will be accessible whenever you require() your config.js.

    This way of creating modules is defined in the CommonJS spec:

    • http://commonjs.org/specs/modules/1.0/
    • http://wiki.commonjs.org/wiki/Modules/1.1

    edit: and since commonjs.org seems to be down, here is another link with information about this type of module: http://dailyjs.com/2010/10/18/modules/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have our first NodeJS server that's being deployed to a client server along
I have simulated a browser client of socket.io in the node.js server I am
I have config.php with this... $dbhost = localhost; I want to be able to
We have multiple config files (app.DEV.config, app.TEST.config, etc) and a pre-build event that copies
I have a config file that I read using the RawConfigParser in the standard
I have app.config in m win application, and loggingConfiguration section (enterprise library 4.1). I
I have seen config files(yes, text files) for various console applications that look like
In a servlet I have a config text file. How to prevent web access
I would like to have individuals config for my rails apps on my server,
I have a web.config of a functioning MOSS server with our custom settings and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.