1

I'm trying to return a value from a class but it's coming up undefined.

index.js

import DB from "./db.js"
import express from "express";
import cors from "cors";

const database = new DB();

app.get("/", (req, res) => {
   const data = database.selectAllFromProducts();
   console.log(data); // Returns undefined
});

app.listen(process.env.PORT, () =>
    console.log(`Listening on Port ${process.env.PORT}`)
);

db.js

class DB {
    constructor() {
        this.connection = this.initialize();
    }

    initialize() {
        return mysql.createConnection({
           host: process.env.DB_HOST,
           user: process.env.DB_USER,
           password: process.env.DB_PASSWORD,
           database: process.env.DB_NAME,
    });

    selectAllFromProducts() {
        this.initialize();

        this.connection.query(`select * from ${process.env.DB_PRODUCTS_TABLE};`,
            (err, results, fields) => {return results});
    }
}

I have a front end that is sending the GET request and that is successful so it's not a routing problem. Console.logging the results works from db.js so I know it's not a MYSQL problem but for whatever reason it comes up blank in index.js. Thanks in advance for any help!

EDIT - I have module.exports = DB I just forgot to include it because I only included partial bits of the file. Importing works just fine for me because I'm using babel and type: module in my package.json. I couldn't tag node because it requires 1500 rep.

  • This is node.js code, you may want to reflect that in your tags. – Rob Monhemius Jul 18 '20 at 04:32
  • This is like many other questions. Your `return results` is inside the `.query()` callback. It's not the return from your function. Your function `selectAllFromProducts()` doesn't actually return anything. That's why the return value is `undefined`. And, in fact, you cannot directly return the `results` from your function because your function returns BEFORE it's available. See [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jfriend00 Jul 18 '20 at 05:44

1 Answers1

0

You forgot to export your module. Thus database should be undefined in your code, resulting in your issues.

class DB {
    constructor() {
        this.connection = this.initialize();
    }

    initialize() {
        return mysql.createConnection({
           host: process.env.DB_HOST,
           user: process.env.DB_USER,
           password: process.env.DB_PASSWORD,
           database: process.env.DB_NAME,
    });

    selectAllFromProducts() {
        this.initialize();

        this.connection.query(`select * from ${process.env.DB_PRODUCTS_TABLE};`,
            (err, results, fields) => {return results});
    }
}

module.exports = DB;

Also importing is a little different in node.js

var DB = require("./db.js");
var express = require("express");
var cors = require("cors");

const database = new DB();

app.get("/", (req, res) => {
   const data = database.selectAllFromProducts();
   console.log(data); // Returns undefined
});

app.listen(process.env.PORT, () =>
    console.log(`Listening on Port ${process.env.PORT}`)
);

Note: You should be able to use javascript modules in node.js versions greater than 13 (which is why your code may be working). Make sure your deployment server supports node.js 13 as well if you decide to use that syntax. More on javascript modules

Rob Monhemius
  • 4,822
  • 2
  • 17
  • 49