TypeError:即使我创建了这个函数,undefined也不是函数

时间:2015-05-25 22:30:44

标签: javascript typeerror

这个项目有相当数量,但我是javascript的新手并且正在获得" TypeError:undefined不是函数"即使我已经使用正确的名称创建了该函数(在不同的javascript文件中)。

错误讯息;

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)

这是它的所在地;

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);
        }
    });
});

这是在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);
    }
};

可能有些傻乎乎的看不到。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

除非我误解,否则你所做的事情很奇怪。节点中最简单的答案是您导出在gameServer.js中使用的任何代码,然后将其存储在应用程序内部的var中。

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

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