Before asking this question, yes I have read the incumbent question here and it still makes no sense to me hence marking this a duplicate is not helpful when the other question does not give me an answer I can understand.
- I want a function to return a value (JSON data from a database).
- Whenever that function is called from another file, I want the value to be returned to the calling page.
- That's it.
So here is my function in my .js
file:
async function rsOffersAll() {
await global.MYDB.connect(); // global variable already setup using mssql module in app.js
try {
global.MYDB.request(global.MYDB).query('SELECT Top(10) * FROM [Broadcast].[Offer]').then(function(result) {
console.dir(result); // shows correct data
return JSON.stringify(result); // how do I return this value so that any other file calling rsOffersAll gets THIS result?
});
} catch (error) {
}
}
module.exports.rsOffersAll = rsOffersAll;
In my calling .js
file I have this:
const Offer = require('../models/broadcasts/offer');
(async function rsOffersAll() {
let rsOffersAll = await Offer.rsOffersAll();
console.log(rsOffersAll); // this should log the JSON data in the console but I get "undefined" instead
})();
How can I edit the above code so that the data is returned to the calling page? I know that for some reason nothing is being returned, but I don't know why (linking to another question that asks a similar question is not going to help, trust me). Could someone be kind enough to explain it using my code above?