简体   繁体   中英

Javascript/Node.js “Function name” is not a function

So I have this function which was working fine yesterday but today when I turned PC on it stopped working. Were there any updates that I am not aware of, did i maybe accidentally delete something, but I haven't touched code since yesterday? Can someone help me find this annoying bug?

Function I'm trying to reach is inside other folder and other script.

Here is the function:

var npcSchema = new mongoose.Schema({
    NPCname: {type: String, unique:true},
    life: Number,
    zone : String,
    pos_x: Number,
    pos_y: Number,
    zgodba: Boolean
});

npcSchema.NarediNpc = function(imeNpc, socket){
    NPC.findOne({NPCname: imeNpc}, function(err, npc){
        socket.write(packet.build([
            "NPC",
            npc.NPCname,
            npc.life,
            npc.zone,
            npc.pos_x,
            npc.pos_y
        ]));
    })
};

module.exports = NPC = gamedb.model('npcs', npcSchema);

And this is the code where I'm trying to access the function:

var npcNaredi = new require('./Models/user.js');
case "NPC":              
    npcNaredi.NarediNpc("obj_npcLograk", c.socket);
    break;

Variables and function names are written in slovene, I hope that won't make problems, it might actually be just some stupid typo but I can't find it and I'm getting headache from this...

I see that you are trying to create an instance method, try this:

npcSchema.methods.NarediNpc = function(imeNpc, socket){
  // ...
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM