实现全球范围

Achieve Global Scope

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

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

奇怪的是,即使定义了错误,也就是它确实列出了所需的唯一标识。我确实尝试将数据的浅层和深层副本复制到一个名为 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));
        });

    }


看起来,如果您将 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; /*...*/ });