R
Reactiflux

✅ – dmikester1 – 16-27 Oct 12

✅ – dmikester1 – 16-27 Oct 12

Ddmikester110/12/2022
Once again, here I am struggling with async/await and promises. I have a Node endpoint that ran a bunch of code to read some files from a directory and do some processing and it was all working just fine. Then I realized I needed to abstract that code out so I could call it from multiple places. And now I cannot get a result back from that endpoint. I know it has to do with how I am handling the promises. Here is the endpoint code:
router.get('/getMisfitCount', async (req, res) => {
console.log('getting misfit count...');
let misfitCountData = null;
try {
misfitCountData = await getMisfitCount();
console.log({ misfitCountData });
} catch (e) {
console.log({ e });
}
res.send(misfitCountData);
});
router.get('/getMisfitCount', async (req, res) => {
console.log('getting misfit count...');
let misfitCountData = null;
try {
misfitCountData = await getMisfitCount();
console.log({ misfitCountData });
} catch (e) {
console.log({ e });
}
res.send(misfitCountData);
});
and here is getMisfitCount():
const getMisfitCount = () => {
const misfitsFolder = process.env.MISFITS_FOLDER;

const EXTENSION = '.json';
fs.promises.readdir(misfitsFolder)
.then((files) => {
const targetFiles = files.filter((file) => {
return path.extname(file).toLowerCase() === EXTENSION;
});
let misfitReasons = {};
targetFiles.forEach((file, index) => {
const rawdata = fs.readFileSync(misfitsFolder + '/' + file);
const misfitData = JSON.parse(rawdata);
// some more logic/processing here
console.log('sending back results');
return { count: 5, misfitResultsMsg: 'hi there' };
})
.catch((e) => {
console.log({ e });
return { count: 0, misfitResultsMsg: '' };
});
};
const getMisfitCount = () => {
const misfitsFolder = process.env.MISFITS_FOLDER;

const EXTENSION = '.json';
fs.promises.readdir(misfitsFolder)
.then((files) => {
const targetFiles = files.filter((file) => {
return path.extname(file).toLowerCase() === EXTENSION;
});
let misfitReasons = {};
targetFiles.forEach((file, index) => {
const rawdata = fs.readFileSync(misfitsFolder + '/' + file);
const misfitData = JSON.parse(rawdata);
// some more logic/processing here
console.log('sending back results');
return { count: 5, misfitResultsMsg: 'hi there' };
})
.catch((e) => {
console.log({ e });
return { count: 0, misfitResultsMsg: '' };
});
};
when I log out misfitCountData in the endpoint, it is undefined and gets output before I see sending back:...
UUUnknown User10/12/2022
3 Messages Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
targetFiles gets used a few lines down: targetFiles.forEach((file, index) => {
UUUnknown User10/12/2022
2 Messages Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
I understand that. I am not accessing targetFiles outside the function.
UUUnknown User10/12/2022
2 Messages Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
that }); is closing the files.filter block
UUUnknown User10/12/2022
Message Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
yeah, sorry about that
UUUnknown User10/12/2022
2 Messages Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
shoot, i deleted my return when i was cleaning up my code
UUUnknown User10/12/2022
2 Messages Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
i edited the function to show my return i see 'sending back results' after i see 'getting misfit count...'
UUUnknown User10/12/2022
3 Messages Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
ohhh, so you are saying i need to do return fs.promises.readdir(misfitsFolder)....
UUUnknown User10/12/2022
Message Not Public
Sign In & Join Server To View
Ddmikester110/12/2022
holy crap, that was it! thank you so much!!! struggled way too long on that one! 🙂
UUUnknown User10/13/2022
3 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – dmikester1 – 16-27 Oct 12

Join Server