【问题标题】:Node.js can't access lexical declaration 'x' before initialization and global variable problemNode.js 在初始化和全局变量问题之前无法访问词法声明'x'
【发布时间】:2021-11-28 09:57:03
【问题描述】:

我正在制作的 node.js 应用程序有问题。这是我第一次使用 node.js,所以我知道会有问题,但到目前为止我无法解决这个问题。我想在单击站点上的按钮后运行脚本,但我 在初始化之前无法访问词法声明“日志” 错误,我认为这是因为我使用的是全局变量,因为控制台也显示此错误消息require is not defined

这是我导出的脚本代码:

let login 
{
    login: Cat;
    haslo: 123;
}

function Logowanie(x, y){
    
        if (x == login.login && y == login.haslo){
            const zmianaTla = document.getElementById('overlay');
            const udanyLogin = document.getElementById('logon');
            zmianaTla.classList.remove('active');
            udanyLogin.remove();
        }
       else
            alert("Niepoprawny login lub haslo");
}
    

module.exports = new Logowanie;

主脚本:

const log=require('./login.js');

x=document.getElementById('login').value;
y=document.getElementById('haslo').value; 

log(x, y)

还有html部分:

<script src="script.js"></script>
<div class="logon" id="logon">
     <h1>Logowanie</h1>
      <label for="Log"><b>Login</b></label>
       <input type="text" id="login" name="login" required>
            <label for="Haslo"><b>Hasło</b></label>
            <input type="password" id="haslo" name="haslo" required>
            <button type="submit" class="b1" onClick="log()"> Login </button>
    </div>
    <div class="active" id="overlay"></div>

我尝试使用其他用户遇到的类似问题的解决方案来解决它,但他们没有工作,所以我最终决定自己寻求帮助。

编辑:我忘了提到我在 Visual Code Studio 中使用命令行运行应用程序。这是代码:

const express = require('express');
const app = express();
const path = require("path");
const http = require("http");
const hostname = '127.0.0.1';
const port = 3000;

app.listen(port, () => console.info('Listening on port 3000'));

app.use(express.static('public'))
app.use(express.static(path.join(__dirname, './public/css')))
app.use(express.static(path.join(__dirname, './public/img')))
app.use(express.static(path.join(__dirname, './public/js')))

app.get('',(req, res) => {
    res.sendFile(path.join(__dirname, './views/index.html'))
})

两个脚本在同一个文件夹中,但在不同的文件中(我使用的是全局变量,所以在现场检查期间function Logowanie() 不可见。

【问题讨论】:

  • 我认为您在这里遇到了两个错误,对吧?一个用于require,一个用于log。请说明主脚本在哪里,第一个代码语句在哪里
  • @Tushar Shani 两个脚本的文件位于不同文件的同一文件夹中。有 2 个错误,一个针对 require,一个针对我导出的 log() 函数。
  • Sweet 这些编辑清楚地说明了发生了什么。我在下面编辑了我的答案来解决。
  • let login { login: Cat; haslo: 123; } 是语法错误。你是什​​么意思,初始化?如果是这样,请添加 = 并删除换行符。
  • 你为什么要new Logowanie;?该函数既不像构造函数,也不传递预期的参数。

标签: javascript html node.js


【解决方案1】:

好的,自从那次编辑后我就明白了。以下是您需要进行的更改才能使其正常工作。如果有任何不清楚的地方请告诉我,我可以帮助澄清。

你走在正确的轨道上,我认为只是模块导入/导出和函数绑定把你搞砸了。波尔?。

有用的文档:

login.js

// = sign was missing
let login = {
    // Cat is a String and needs quotes. Semicolons need to be commas in js object declarations
    login: "Cat",
    haslo: 123
}

function Logowanie(x,y){
    // should use === here not ==
    if (x === login.login && y === login.haslo) {
        // your code
    }
}

// Export your function from this file. This is ES6 module syntax supported in modern browsers. FYI you cannot use CommonJS style module.exports in a browser without Webpack or similar setup.
export default Logowanie;

script.js

// Using ES6 module syntax to import your function. Note that you don't actually have to name it Logowanie as we used the default export.
import Logowanie from "./login.js";

// because we are using ES6 module syntax, you need to define the "log" function on the window object of the webpage. This is so the browser knows what to do if when we call log() onclick basically.
window.log = () => {
    let x = document.getElementById("login").value;
    let y = document.getElementById("haslo").value;

    // Logwanie function in login.js is expecting y to be an int due to === comparison.
    Logowanie(x, parseInt(y));
};

index.html

<!-- the type="module" is needed to use ES6 module syntax -->
<script type="module" src="script.js"></script>

<!-- the rest of your HTML was fine I believe -->

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2020-06-29
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2020-01-15
    • 2020-08-25
    相关资源
    最近更新 更多