导入的功能不做在运行开玩笑时应该做的事情

发布于 2025-02-10 15:11:04 字数 2244 浏览 2 评论 0原文

数据存储文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2025-02-17 15:11:04

我认为您面临的问题是您在./ 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文