DATA STORE FILE:
let data = {
users: [],
channels: [],
};
// Use get() to access the data
function getData() {
return data;
}
// Use set(newData) to pass in the entire data object, with modifications made
function setData(newData) {
data = newData;
}
export { getData, setData };
clearV1() FILE:
import { getData, setData } from './dataStore';
function clearV1() {
let data = {
users: [],
channels: [],
};
setData(data);
return {};
}
export { clearV1 };
When running the clearV1() function in another, it does not clear the data store. For example: authRegisterV1 creates a user and adds them to the data store channelCreateV1 creates a channel and adds it to the data store
authRegisterV1('test1@gmail.com','test123','Firt','Last');
clearV1()
authRegisterV1('test2@gmail.com','test123','Firt','Last');
expected output:
{
users: [
{
uId: 1,
email: 'test2@gmail.com',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast0',
permissionId: 2
}
],
channels: [],
}
wrong output:
{
users: [
{
uId: 1,
email: 'test1@gmail.com',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast',
permissionId: 1
},
{
uId: 2,
email: 'test2@gmail.com',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast0',
permissionId: 2
}
],
channels: [],
}
I believe the implementation of the clearV1() function is correct, what other possible reason could there be for this error? I imported all the used functions into the test file.
I think the problem your facing is the fact that you created data inside of ./dataStore, and thus, it does not exist in clearV1() file. Another way to put is, when you made the data variable initially, it was made in ./dataStore and only exists there. So it makes a new variable instead of updating the existing one.
Another problem is, you are trying to using let data = [value]. let creates the variable just inside of the function you called it in, ignoring any variables on the outside. As a rule of thumb in javascript, when updating an existing variable, use [name] = [value].
If you want to learn more, here's the MDN docs for import statements and let statements.