简体   繁体   English

TypeError:即使我创建了此函数,undefined不是一个函数

[英]TypeError: undefined is not a function even though i created this function

There is a fair amount to this project, but I am new to javascript and am getting "TypeError: undefined is not a function" even though I have created the function with the correct name (in a different javascript file). 这个项目有很多,但是我是javascript的新手,即使我使用正确的名称(在另一个javascript文件中)创建了函数,但我还是收到“ TypeError:未定义不是函数”。

Error message; 错误信息;

C:\Users\Matt\Documents\GitHub\SnailsBattleground>node app.js
Server listening to port 9001
Attempt to load C:\Users\Matt\Documents\GitHub\SnailsBattleground/index.html
express deprecated res.sendfile: Use res.sendFile instead app.js:24:6
express deprecated res.sendfile: Use res.sendFile instead app.js:33:6
C:\Users\Matt\Documents\GitHub\SnailsBattleground\app.js:58
    gameServer.findGame(client);
               ^
TypeError: undefined is not a function
at Namespace.<anonymous> (C:\Users\Matt\Documents\GitHub\SnailsBattleground\
app.js:58:13)
at Namespace.emit (events.js:107:17)
at Namespace.emit (C:\Users\Matt\Documents\GitHub\SnailsBattleground\node_mo
dules\socket.io\lib\namespace.js:205:10)
at C:\Users\Matt\Documents\GitHub\SnailsBattleground\node_modules\socket.io\
lib\namespace.js:172:14
at process._tickCallback (node.js:355:11)

Here is where it is called; 这就是所谓的地方。

socket.sockets.on("connection", function(client) {
    client.userid = UUID();

    client.emit("onconnected", {
        id: client.userid
    });

    gameServer.findGame(client);

    console.log("New player " + client.userid + "has joined.");

    client.on("message", function(m) {
        gameServer.delayMessage(client, m);
    });

    client.on("disconnect", function() {
        console.log("Player " + client.userid + "has disconnected.");

        //end the game if the client is disconnected 
        if (client.game && client.game.id) {
            gameServer.endGame(client.game.id, client.userid);
        }
    });
});

And here it is in gameServer.js; 这是在gameServer.js中;

gameServer.findGame = function(player){
    this.log('looking for a game. We have : ' + this.gameCount);
    if(this.gameCount){
        var joinGame = false;

            for(var gameId in this.games) {
            if(!this.games.hasOwnProperty(gameId)) continue;

            var gameInst = this.games[gameId];

            if(gameInst.playerCount < 2) {
                joinGame = true;

                gameInst.playerClient = player;
                gameInst.gameMg.players.other.instance = player;
                gameInst.playerCount++;

                this.startGame(gameInst);
            }
        }
        if(!joinGame){
            this.createGame(player);
        }
    }
    else {
        this.createGame(player);
    }
};

It is likely some silly can't see though. 可能有些傻看不见。 Thanks for any help 谢谢你的帮助

Unless I'm misunderstanding, what you're doing is odd. 除非我有误会,否则你在做什么很奇怪。 The simplest answer in node is for you to export whatever code you are using in gameServer.js and then store it in a var inside of your app. 节点中最简单的答案是让您导出gameServer.js中使用的任何代码,然后将其存储在应用程序内部的var中。

//in gameServer.js
exports.findGame = function(){//do Stuff};

//in App.js
var gameServer = require("./pathToFile/gameServer.js");
gameServer.findGame();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义 Function 即使我在 Javascript 中定义了它 - Undefined Function Even though I defined it in Javascript 即使将函数添加到构造函数的原型中,为什么也得到“函数未定义”的信息? - Why do I get a 'function is undefined' even though I added the function to the constructor's prototype? 我已经在jQuery中收到函数未定义错误,即使该函数已定义 - I am getting function undefined error in jQuery, even though the function is defined “无法读取未定义的属性‘映射’”,即使我明确告诉它如果数组未定义则不要运行 function - 'Cannot read property 'map' of undefined' even though I explicitly told it not to run the function if array is undefined 未捕获的TypeError:undefined不是函数,在for循环中创建的对象 - Uncaught TypeError: undefined is not a function, object created in for loop 未捕获的TypeError:$(...).ready(...)不是函数,即使它适用于JsFiddle - Uncaught TypeError: $(…).ready(…) is not a function even though it works on JsFiddle 函数未定义,即使我定义了它 - Function not defined, even though I defined it 即使我有一个数组,map 也不是 function - map is not a function even though I have an array Function 未定义,即使它是 - Function is not defined even though it is 函数没有定义,即使它是 - function not defined even though it is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM