简体   繁体   English

在循环JS中引发错误

[英]Throwing errors in loop JS

I have some toggled feature values on backend and I need to compare them with some values on frontend side. 我在后端有一些已切换的功能值,我需要将它们与前端上的一些值进行比较。 This way website decides if it should show calendar, menu... (it's not the issue here). 网站通过这种方式决定是否应显示日历,菜单...(这不是这里的问题)。

From backend I get array for let say seven simple objects: 从后端我得到了可以说七个简单对象的数组:

[
  {name: "menu1"; value: "password"}
  {name: "menu2"; value: "password"}
  ...
  {name: "menu7"; value: "password"}
]

On front end I have an object: 在前端,我有一个对象:

  {
    "menu1": true;
    "menu1": true;
    ...
    "menu6": true;
  }

I'm using axios to get this data. 我正在使用axios来获取此数据。 Problem I have is this: even if I have 6 values that are present on frontend, whole website will crash, because of this one feature that don't exist. 我的问题是:即使前端有6个值,整个网站也会崩溃,因为该功能不存在。 It's working as intended, but I would like to throw correct error. 它按预期工作,但我想抛出正确的错误。 Here's the code: 这是代码:

  loadAttributes(URL) {
    return axios.get(URL)
      .then(res => {
          const {attributes} = res.data;
          const toggles = {};
          for (let i = 0; i < attributes.length; i++) {
              if (Constants.featureToggleMDs[attributes[i].name] === undefined) {
                 throw (new Error(`There is a feature: ${attributes[i].name} in ACS that is not presented in constants.`));
              }
              const left = Constants.featureToggleMDs[attributes[i].name].toUpperCase();
              const right = sha512(attributes[i].value).toUpperCase();

              toggles[attributes[i].name] = (left === right);
            }
            return toggles;
        }).catch(e => console.error(`[Feature Toggle] Attributes not supported: ${e}`));
  }

I have an condition to check if value exists in frontend object, and when debugging, I only enter this part of the code once for "menu7". 我有一个条件检查前端对象中是否存在值,并且在调试时,我只对“ menu7”输入了这部分代码一次。 Unfortunately error is thrown 7 times. 不幸的是,错误被抛出7次。 And that's the issue here. 这就是这里的问题。 How and why it's thrown and caught 7 times? 如何以及为什么将其抛出并捕获7次? Is this an axios issue? 这是axios问题吗? I tried simplify it with snippet below and it's working correctly. 我尝试使用下面的代码片段简化它,并且可以正常工作。 But not in my project. 但是不在我的项目中。 Any ideas? 有任何想法吗?

 let attributes = [ { name: 'menu1', value: 'one' }, { name: 'menu2', value: 'two' }, { name: 'menu3', value: 'one' }, { name: 'menu4', value: 'two' }, { name: 'menu5', value: 'one' } ]; defaultFeatureToggles = { 'menu1': false, 'menu2': false, 'menu3': false, 'menu4': false }; for (let i = 0; i < attributes.length; i++) { if(defaultFeatureToggles[attributes[i].name] === undefined) { console.warn(`This ${attributes[i].name} don't exists.`); } } 

The reason for my multiple throws was simple. 我多次掷球的原因很简单。 I used this function in other place I forgot about it. 我在其他地方忘记了使用此功能。 Everything works fine now. 现在一切正常。 Sorry for space wasting. 很抱歉浪费空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM