Initially I have an empty array inside of the file, but during the code execution I get data from the database and push the data to an array. My code looks like this:
items.js
const items = [];
const init = (connection) => {
return connection.collection('collectionName')
.aggregate([{
....
}])
.toArray()
.then(result => {
items.push(...result);
return result;
});
};
module.exports = {
init,
all: items,
item1: items[0],
item2: roles[1],
};
And when I try to import all, item1 and item2 using require in a different file like this:
const { all, item1, item2 } = require('../data/items');
I get the array with object as all, but get undefined for item1 and item 2. I can't seem to understand what the issue is. I know I can use an anonumous function when exporting and get the data this way, but I would rather do it by trying to access the data from an array by element's index. All the help is much apprecieted!