我正在使用Node.js声明一个函数,但是它是不可调用的。

我不明白这是什么错误,因为我以与其他函数相同的方式声明了它,这是唯一一个不可调用的函数。

我的代码:

const mysql = require('mysql');
const mainKey = '';
const con = mysql.createConnection({
    host: "localhost",
    user: "root",
    database: "hotels"
});



function getUsers() {
    return new Promise(function(resolve, reject) {
        var users = new Array();
        const sql = "SELECT * FROM users";
        con.query(sql, function(err, result, fields) {
            if(err) throw err;
            users = [];
            for(var i = 0; i<result.length; i++) {
                users.push([result[i].id, result[i].user, result[i].password]);
            }
            resolve(users);
        });
    });
}

function regUser(user, password, key) {  //this function is non callable
    return new Promise(function(resolve, reject){
        console.log(test)
    });
}

function getHotelNames(idUser) {
    return new Promise(function(resolve, reject){
        var hotelNames = new Array();
        const sql = "SELECT * FROM hotels WHERE user=" + idUser;
        con.query(sql, function (err, result, fields) {
            if (err) throw err;
            hotelNames = [];
            for(var i=0; i<result.length; i++) {
                hotelNames.push(escape(result[i].name));
            }
            resolve(hotelNames);
        });
    })
}

function getURLs() {
    return new Promise(function(resolve, reject){
        var urlsHotels = new Array();
        const sql = "SELECT * FROM hotels";
        con.query(sql, function (err, result) {
            if (err) throw err;
            urlsHotels = [];
            for(var i=0; i<result.length; i++) {
                urlsHotels.push(result[i].url);
            }
            resolve(urlsHotels);
        });
    })
}

function insertValues(hotelNames, url, name, idUser) {
    return new Promise(function(resolve, reject){
        const id = hotelNames.length+1
        const sql = "INSERT INTO hotels (id, name, url, user) VALUES (" + id + ", '" + name + "', '" + url + "', " + idUser +")";
        con.query(sql, function (err, result) {
            if (err)  resolve(['errorNewHotel']); //throw err;
            resolve(['NewHotel'])
        });
    })
}

function deleteValues(name) {
    return new Promise(function(resolve, reject){
        const sql = "DELETE FROM hotels WHERE name = '" + name + "'";
        console.log(sql)
        con.query(sql, function (err, result) {
            if (err)  resolve(['errorDeletingHotel']); //throw err;
            resolve(['deletedHotel'])
        });
    })
}

const funcGetUsers = async ()=> {
    const users = await getUsers();
    return users;
}

const funcRegisterUser = async (user, password, key)=> {
    const regUser = await regUser(user, password, key); //I am calling the function here
    return regUser;
}

const funcGetHotelNames = async (idUser)=> {
    const hotelNames = await getHotelNames(idUser);
    return hotelNames;
}

const funcGetURLs = async ()=> {
    const urls = await getURLs();
    return urls;
}

const funcInsertValues = async(hotelNames, url, name, idUser)=> {
    const message = await insertValues(hotelNames, url, name, idUser);
    return message;
}

const funcDeleteValues = async(name)=> {
    const message = await deleteValues(name);
    return message;
}

module.exports.funcGetUsers = funcGetUsers;
module.exports.funcRegisterUser = funcRegisterUser;
module.exports.funcGetHotelNames = funcGetHotelNames;
module.exports.funcGetURLs = funcGetURLs;
module.exports.funcInsertValues = funcInsertValues;
module.exports.funcDeleteValues = funcDeleteValues;


甚至我的代码编辑器Visual Studio Code都说它永远不会被读取


这是我尝试运行功能时得到的:



我认为问题不在于主文件(server.js),所以我尝试将所有代码复制到一个新文件中,Visual Studio仍在说其他功能都还不错时,它永远不会被读取。

我究竟做错了什么?我不明白。

先感谢您。

最佳答案

真正的Javascript错误是:


  ReferenceError:未定义regUser




const funcRegisterUser = async (user, password, key)=> {
    const regUser = await regUser(user, password, key); //I am calling the function here
    return regUser;
}


您试图分配给名为regUser的变量,同时还要调用名为regUser的函数。但是,由于您在该函数内声明了一个名为regUser的变量,因此对该函数内对名为regUser的变量的任何引用都将引用该regUser变量。因此,您的await regUser(...)试图await尚未分配给它的变量-它位于时间死区中。

只需使用其他变量名,就可以避免名称冲突:

const funcRegisterUser = async (user, password, key)=> {
    const result = await regUser(user, password, key);
    return result;
}


或者,仅返回Promise本身,无需await立即返回的内容:

const funcRegisterUser = (user, password, key) => (
    regUser(user, password, key)
);


或者,甚至更好的是,由于funcRegisterUser只是调用regUser,所以可能完全不使用funcRegisterUser,而只是导出regUser

module.exports.funcRegisterUser = regUser;

08-28 01:10