导入的功能不做在运行开玩笑时应该做的事情
数据存储文件:
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()文件:
import { getData, setData } from './dataStore';
function clearV1() {
let data = {
users: [],
channels: [],
};
setData(data);
return {};
}
export { clearV1 };
在另一个文件中运行clearv1()函数时,它不会清除数据存储。例如: AuthRegisterv1创建用户并将其添加到数据存储 ChannelCreatev1创建一个频道并将其添加到数据存储中,
authRegisterV1('[email protected]','test123','Firt','Last');
clearV1()
authRegisterV1('[email protected]','test123','Firt','Last');
expected output:
{
users: [
{
uId: 1,
email: '[email protected]',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast0',
permissionId: 2
}
],
channels: [],
}
wrong output:
{
users: [
{
uId: 1,
email: '[email protected]',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast',
permissionId: 1
},
{
uId: 2,
email: '[email protected]',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast0',
permissionId: 2
}
],
channels: [],
}
我相信Clearv1()函数的实现是正确的,此错误还有什么其他可能的原因?我将所有使用的功能导入到测试文件中。
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('[email protected]','test123','Firt','Last');
clearV1()
authRegisterV1('[email protected]','test123','Firt','Last');
expected output:
{
users: [
{
uId: 1,
email: '[email protected]',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast0',
permissionId: 2
}
],
channels: [],
}
wrong output:
{
users: [
{
uId: 1,
email: '[email protected]',
password: 'test123',
nameFirst: 'Firt',
nameLast: 'Last',
handle: 'firtlast',
permissionId: 1
},
{
uId: 2,
email: '[email protected]',
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您面临的问题是您在
./ Datastore
的内部创建了数据
,因此,它在clearv1()文件。另一种方法是,当您最初制作数据变量时,它是用
./ DataStore
制成的,并且仅存在。因此,它制造了一个新变量,而不是更新现有变量。另一个问题是,您正在尝试使用
LET DATA = [value]
。让
在您调用的函数的内部创建变量,忽略了外部的任何变量。根据JavaScript中的经验,更新现有变量时,请使用[name] = [value]
。如果您想了解更多信息,这是“ noreferrer”>“ noreferrer”>“ noreferrer”>导入语句 and 让语言。
I think the problem your facing is the fact that you created
data
inside of./dataStore
, and thus, it does not exist inclearV1()
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.