Reputation: 93
I have an async function called "teteraQueryDb()" that fetches data from a Db in a route like :/root/afolder/bfolder/file.js The function is exported like: module.exports.teteraQueryDb = teteraQueryDb
When I try to call the function from a file in the same route I do this and is succesfull:
const teteraProvDb = require('./readingProvTetera');
(async(teteraProvDb) =>{
var newTeteraProvDb = await teteraProvDb.teteraQueryDb().then((te) => {return te})
console.log(newTeteraProvDb)
})(teteraProvDb);
But doing this from the root, I can't fetch the data and I get "undefined":
const teteraProvDb = require('./afolder/bfolder/readingProvTetera');
(async(teteraProvDb) =>{
var newTeteraProvDb = await teteraProvDb.teteraQueryDb().then((te) => {return te})
console.log(newTeteraProvDb)
})(teteraProvDb);
This is the configuration of readingProvTetera.js:
const sqlite3 = require('sqlite3').verbose();
const dbRoute = '../../myDb.db'
function teteraQueryDb(){
let db = new sqlite3.Database(dbRoute, err =>{
console.log(err)
})
return new Promise (resolve =>{
db.all("SELECT DISTINCT * FROM tetera", (err, rows) =>{
if(err){
console.log("Something went wrong with the query");
}
resolve(rows)
})
})
db.close()
}
module.exports.teteraQueryDb = teteraQueryDb
Why? Am I missing somethig with the scopes?
Upvotes: 0
Views: 121
Reputation: 1090
./
isn't root path but current folder path. So when you write path ./readingProvTetera
it means loading the db from current folder. And path ./afolder/bfolder/readingProvTetera
means loading the db from current folder's afolder's bfolder.
The root path can be checked by console.log(process.env.PWD)
. So the path can be ${process.env.PWD}/afolder/bfolder/file.js
.
Upvotes: 1