I'm using a map with key: password, value: Guest object to store guest information in my program. At the end of my program I pass an array of the guest Objects to a function to write to my txt file:
writeToGuestStore('./inputFiles/guestStore.txt', Array.from(guestStore.values()));
writeToGuestStore function:
function writeToGuestStore(file, guests) {
//console.log(guests);
const writeFile = fs.createWriteStream(file);
writeFile.on('error', err => console.log(err));
for(let i = 0; i < guests.length; i++) {
let elem = guests[i];
//console.log(elem);
let password = elem.getPassword();
let visit = elem.getVisitCount();
let orders = elem.getOrderStore();
//console.log(password);
//console.log(visit);
//console.log(orders);
writeFile.write(/* elements of guest */);
}
writeFile.end();
}
I believe that my guest objects are being passed properly to the function, by which I mean all their properties are defined as they should, however the lines where I get the password, visitCount, and orderStore values only seem to return the correct values for the last element in the array, returning undefined for all other guests:
Guest {
name: 'ethan',
allergyInfo: [],
nutrients: {},
money: 9,
[Symbol(visitCount)]: 0,
[Symbol(password)]: 4268,
[Symbol(orderStore)]: Map(0) {},
[Symbol(addOrder)]: [Function (anonymous)]
}
undefined
undefined
undefined
vs.
Guest {
name: 'elliot',
allergyInfo: [],
nutrients: {},
money: 4,
[Symbol(visitCount)]: 0,
[Symbol(password)]: 4267,
[Symbol(orderStore)]: Map(0) {},
[Symbol(addOrder)]: [Function (anonymous)]
}
4267
0
Map(0) {}
Prior to using a .txt file I attempted the same guest storing with a JSON file with replacer and reviver functions to customize how the properties are written / received, however I was running into the same issue in which only the last guest's values for password, visitCount, and their orderStore were accessed properly.
Any suggestions?