简体   繁体   中英

Value returned from custom database connection class is 'undefined', using node.js and ES6

Edit: I can see that the other question is probably dealing with the same problem, but I can't understand how to apply the solution given to his function foo(){..} to my specific problem (not least because his is jQuery). If someone can explain it here, then I'll mark this question as a duplicate.

In the custom class the value is accessible, but in the the main code block the return value is 'undefined'. Is this some synchronisation issue due to the database connection? Can I somehow get access to that value in the main index.js calling function?

index.js:

"use strict"
const PORT = 3000;
const express = require("express");
const server = express();
const http = require("http").Server(server);
const path = require("path");
const io = require("socket.io")(http);
const DB = require("./ofcModules/DB");

http.listen(PORT, function() {
      console.log("server listening on port " + PORT);
});

//serve index.html (and CSS/JS) when receives a request
server.use(express.static(path.join(__dirname + "/public")));
server.use(express.static(__dirname + "/public/css"));
server.use(express.static(__dirname + "/public/js"));

//*********Connect to Database************************
var mDB = new DB();
var mTblID;

mDB.connect()
mTblID = mDB.newTable();
console.log("mTblID: " + mTblID);

custom DB class:

"use strict"
const mysql = require("mysql");

class DB{

      constructor() {
            this.conn = mysql.createConnection({
                  host  : 'localhost',
                  user  : "hfghr",
                  password: "dhrjtyjnm",
                  database: "yiluilrt"
            }); 
      }

      connect() {
            this.conn.connect(function(err) {
                  if (err) {
                        console.error("error connecting: " + err.stack);
                        //throw err;
                        return;
                  }
                  console.log("connected to DB");
                  return this.conn;
            });
      }

      newTable() {
            this.conn.query("INSERT INTO tbltables (notes) VALUES ('table opened')", function(err, result){
                  if (err) {
                        console.error("error getting table ID from mysql: " + err.stack);
                        throw err;
                        return;
                  }
                  console.log("playing table created via DB. ID: " + result.insertId);

                  return result.insertId;
            });
      }

}

module.exports = DB

Here's the server console output:

mTblID: undefined
server listening on port 3000
connected to DB
playing table created via DB. ID: 18

Your newTable function is not actually returning any value. Since mysql.query is async, you need to provide a callback. A solution would be to pass a function to newTable, which it will execute within the mysql.query callback. Here is a rough example:

//*********Connect to Database************************
var mDB = new DB();

mDB.connect()
mDB.newTable(function (mTblID) {
  console.log("mTblID: " + mTblID);
});

Here is the implementation of newTable

newTable(callback) {
  this.conn.query("INSERT INTO tbltables (notes) VALUES ('table opened')", function(err, result){
    if (err) {
      console.error("error getting table ID from mysql: " + err.stack);
      throw err;
      return;
    }
    console.log("playing table created via DB. ID: " + result.insertId);

    callback(result.insertId);
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM