实现全球范围

时间:2020-05-28 01:02:05

标签: javascript arrays json object scope

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

我在Firefox中收到JS控制台错误:“ ReferenceError:未定义BUUS123US163”。

奇怪的是,即使定义了错误,也就是确实列出了所需的唯一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));
        });

    }


picture of html, code, and console

1 个答案:

答案 0 :(得分:0)

看来,如果将id作为简单参数传递给reject和ok函数,则需要用引号将其引起来,从而得出reject ("some-id")reject(some-id),为了使它正常工作,第224行上的代码应该是

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

vs

`<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; /*...*/ });

中手动设置
相关问题