I have the functions below and I’m getting an error in the first function, saying that “getID” is not defined, despite being in module.exports and being imported. I am sure I’m importing the correct file. I have two files below:
authTokenGenerator:
const accounts = require('./queries/accounts.js');
const alph = ["a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z" ];
module.exports = {
authTokenGenerator: (email) => {
return new Promise((resolve) => {
(async () => {
var auth = "";
for (var i = 0; i < 10; i++) {
if (Math.floor(Math.random() * 2) == 0) auth += alph[Math.floor(Math.random() * 25)]
else auth += Math.floor(Math.random() * 10);
}
if (!isEmpty(email)) {
var id = await accounts.getId(email);
var tokenStored = await accounts.storeSessionToken(id, auth, expirationDateTime());
if (tokenStored) return resolve(auth);
else return resolve("An error has occured")
}
return resolve(auth);
}) ();
})
}
accounts.js:
module.exports = {
getId: (email) => {
return new Promise((resolve) => {
(async () => {
var id = await getId(email);
return resolve(id);
}) ();
})
}
}
function getId(email) { // I tried renaming this function but that didn't work
return new Promise((resolve) => {
var getId = `SELECT id FROM accounts WHERE email="${email}"`;
db.query(getId, (err, res) => {
if (err) throw err;
return resolve(res[0].id);
})
})
}
There are more functions in each of these files than are included in this post. In the file accounts.js
I have the getId
function in module.exports
, which calls the function getId
outside of the exports. Below is the error:
var id = await accounts.getId(email);
^
TypeError: accounts.getId is not a function
at /Users/name/Desktop/name/Portal/API/generator.js:19:45
at /Users/name/Desktop/name/Portal/API/generator.js:28:16
at new Promise (<anonymous>)
at Object.authTokenGenerator (/Users/name/Desktop/name/Portal/API/generator.js:9:16)
at resolve (/Users/name/Desktop/name/Portal/API/schema.js:92:56)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async graphqlMiddleware (/Users/name/Desktop/name/Portal/API/node_modules/express-graphql/index.js:125:26)
Node.js v21.6.2
[nodemon] app crashed - waiting for file changes before starting...