【问题标题】:Achieve Global Scope实现全球范围
【发布时间】:2020-09-15 05:04:09
【问题描述】:

我有某种无法解决的范围问题。这幅画值一千字。当 OK 或 REJ 按钮调用 reject() 函数时,将 user_id 作为 id 作为参数传递给拒绝函数。

我在 Firefox 中收到 JS 控制台错误:“ReferenceError: BUUS123US163 is not defined”。

奇怪的是,甚至定义了错误,也就是说,它确实列出了所需的唯一 ID。我确实尝试将数据的浅层和深层副本放入一个名为 theUsers 的全局数组中,但它还没有工作。对于我认为是范围问题的问题,我遗漏了什么?

更新: JSON.parse 错误 在reject()函数的.catch子句中

// function getting: JSON.parse error

    function reject(id) {

      console.log("hey: reject ran... ");
      // User Data
      const data2 = {
        user_id: '',
        utility: 'delete'
      }

      data2.user_id = id;

      // // POST  :  pass user
      http.post('http://localhost:9999/Password/empapproval', data2)
        .then(data => console.log(data))
        .catch(err => console.log(err));

    }

// http.post calls the following:

    post(url, data) {

        console.log("easyHttp is running ...");


        return new Promise((resolve, reject) => {

            console.log("easyHttp Promise is running ...");

            fetch(url, {
                method: 'POST',
                headers: {
                    'Content-type': 'application/json'
                },
                body: JSON.stringify(data)
            })
                .then(res => res.json())
                .then(data => resolve(data))
                .catch(err => reject(err));
        });

    }


【问题讨论】:

  • 请将您的代码发布为纯文本,而不是图像。
  • this.id 应该是什么? reject() 不是方法,它没有 this 上下文。
  • 我认为您需要在 '${}' 周围加上引号,因此它不会将您的数据视为变量名。
  • 也许this.id 应该是id
  • @DaveAnkin 将其作为答案发布。

标签: javascript arrays json object scope


【解决方案1】:

看起来,如果您将 id 作为简单参数传递给 reject 和 ok 函数,则需要引用它才能以这种方式进行评估,从而导致reject ("some-id") vs reject(some-id),以便处理第 224 行的代码应该是

`<td><button onclick="reject('${d.user_id}')" class="" ...`

`<td><button onclick="reject(${d.user_id})" class="" ...`

另外,this.id 没有多大意义,因为 this 上下文仅在您使用 jquery 事件侦听器时为您提供对元素的引用,而不是 html“onclick”属性侦听器。看来它应该只是id,函数的参数,或者你想手动插入HTML后做$(el).on('click', function() { var val = this.dataset.value; /*...*/ });

【讨论】:

  • 进展。由于这样做: onclick="reject('${d.user_id}')" 我现在有错误: SyntaxError: "JSON.parse: unexpected end of data at line 1 column 1 of the JSON data" empApproveRej.html:296 :31
  • 您的后端似乎给您一个错误,而不是您想要的 json。在您发布的屏幕截图中,第 296 行是看不见的。
  • 计算机告诉你,你得到的东西不是 json。 console.log 看看它是什么
  • 非常感谢您的专业知识。解决了。​​
猜你喜欢
  • 1970-01-01
  • 2014-04-09
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 2021-06-07
  • 2020-07-11
  • 2014-02-20
相关资源
最近更新 更多