为什么我的模块函数在node.js中返回未定义?

时间:2019-04-19 21:12:49

标签: javascript node.js json function module

因此,我想将所有mysql查询函数移至另一个文件并将其作为模块导入,但是我遇到麻烦,因为该函数返回的是undefined。

index.js:

const express = require('express')
const https = require('https');
const fs = require('fs');
const dbx = require('./databaseconn');

var options = {
    key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem'),
    ca: fs.readFileSync('/etc/letsencrypt/live/example.com/chain.pem')
};

const app = express();

app.use(express.json());

app.use(function(req,res,next) {
    if (req.body.action) {
        switch (req.body.action) {
            case "login":
                var user = req.body.username;
                var pass = req.body.password;
                console.log(dbx.checklogin(user, pass));  // This prints undefined
                break;
            default:
                break;
        }
    }
});

https.createServer(options,app).listen(8000);

databaseconn.js:

const mysql = require('mysql');

exports.checklogin = function(username, password) {
    var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'xxxxxxxxxxxxxx',
        database : 'users',
    });

    connection.query('select * from logins where username = ? and password = ?;',
    [
        username,
        password
    ], function (err, rows, fields) {
        if (err) throw err;
        if (!rows.length) {
            console.log('0');
            return {"success":0};
        } else {
            console.log('1');      // THIS PRINTS
            return {"success":1};  // So this should return the JSON
        }
    })
}

因此,我在databaseconn.js中的console.log中看到一个“ 1”,但是index.js文件中的console.log正在打印未定义。我究竟做错了什么?我还进行了超级准系统测试,效果很好,所以看不到区别。

test.js:

const express = require('express');
const test2 = require('./test2');
var t = test2.test();
console.log(t);

test2.js:

exports.test = function() {
    return {"success":1};
}

2 个答案:

答案 0 :(得分:2)

console.log模块中的

index.js语句将打印undefined,因为checklogin模块中的databaseconn.js函数不会返回任何内容({"success":1}是返回值checklogin中内部匿名函数的名称,但它不是checklogin本身的返回值)。

如果要在index.js模块中处理查询结果,可以将回调从index.js传递到databaseconn.js模块,如下所示:

index.js:

app.use(function(req, res, next) {
  if (req.body.action) {
    switch (req.body.action) {
      case "login":
        var user = req.body.username;
        var pass = req.body.password;
        dbx.checklogin(user, pass, function(err, rows, fields) {
          console.log('Here you can handle the query result')
        });
        break;
      default:
        break;
    }
  }
});

databaseconn.js:

exports.checklogin = function(username, password, callback) {
  var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'xxxxxxxxxxxxxx',
    database : 'users',
  });

  connection.query('select * from logins where username = ? and password = ?;',
    [
      username,
      password
    ], callback)
}

答案 1 :(得分:1)

您似乎正在从connection.query返回{“ success”:1},但是checklogin函数本身未返回任何信息?

尝试

const mysql = require('mysql');

exports.checklogin = function(username, password) {
    var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : 'xxxxxxxxxxxxxx',
        database : 'users',
    });

    return connection.query('select * from logins where username = ? and password = ?;',
    [
        username,
        password
    ], function (err, rows, fields) {
        if (err) throw err;
        if (!rows.length) {
            console.log('0');
            return {"success":0};
        } else {
            console.log('1');      // THIS PRINTS
            return {"success":1};  // So this should return the JSON
        }
    })
}