【问题标题】:TypeError in mongoose猫鼬中的类型错误
【发布时间】:2013-11-30 06:24:19
【问题描述】:

我正在处理任务并试图让我们今天在教程中使用的猫鼬代码工作。 不过我遇到了一个错误,我不知道为什么。

index.js:


var fs = require('fs');
var mongoose = require('mongoose');
var db = mongoose.connection;
var Rooms2406;

var setupRoomDB = function () {
    var roomSchema = mongoose.Schema({
    title: String,
    description: String,
    roomExits: [String]
    });
    Rooms2406 = mongoose.model("Rooms2406", roomSchema);
}
db.once('open', setupRoomDB);


var addRooms = function(){
    /*to do a check each time a room is added*/

    var saveCallbackFunc = function(err, theRoom){
            var status;
            if(err)
                status = 'fail';
            else{
                status = 'sucess';
            }
            console.log(status);
    }

    /*go through the rooms and add each one*/
    debugger;

    /*THIS IS WHERE THE ISSUE IS*/
    var bridge = new Rooms2406({
    title: "Bridge",
    description: "blah",
    roomExits: ['sickbay']});

    debugger;

    var engineering = new Rooms2406({
    title: "Engineering",
    description: "blah.",
    roomExits: ['sickbay']
    });  

    var sickbay = new Rooms2406({
    title: "Sickbay",
    description: "blah",
    roomExits: ['engineering','bridge']
    });

    var secret= new Rooms2406({
    title: "Secret Room",
    description: "blah",
    roomExits: ['engineering', 'sickbay', 'bridge']
    });

    var TheVoid= new Rooms2406({
    title: "The Void",
    description: "blah.",
    roomExits: ['TheVoid']
    });

    /*saving each room*/
    bridge.save(saveCallbackFunc);
    engineering.save(saveCallbackFunc);
    sickbay.save(saveCallbackFunc);
    secret.save(saveCallbackFunc);   
    TheVoid.save(saveCallbackFunc);

然后在 start() 函数中调用 addRooms() 将房间保存到数据库中。

function start(req, res) {
/*add rooms to mongodb*/
    addRooms();
    var player = req.body.player;
    // Sanitize the player name as it may be displayed
    req.session.player = sanitize(player).escape();
    req.session.currentRoom = "bridge"; // We start on the bridge
    res.redirect("/game")
}

但我得到了错误:


TypeError: undefined is not a function
    at addRooms (/home/student/a4/adventure-ajax-demo/routes/index.js:45:15)
    at start (/home/student/a4/adventure-ajax-demo/routes/index.js:100:2)
    at callbacks (/home/student/a4/adventure-ajax-demo/node_modules/express/lib/router/index.js:164:37)
    at param (/home/student/a4/adventure-ajax-demo/node_modules/express/lib/router/index.js:138:11)
    at pass (/home/student/a4/adventure-ajax-demo/node_modules/express/lib/router/index.js:145:5)
    at Router._dispatch (/home/student/a4/adventure-ajax-demo/node_modules/express/lib/router/index.js:173:5)
    at Object.router (/home/student/a4/adventure-ajax-demo/node_modules/express/lib/router/index.js:33:10)
    at next (/home/student/a4/adventure-ajax-demo/node_modules/express/node_modules/connect/lib/proto.js:190:15)
    at next (/home/student/a4/adventure-ajax-demo/node_modules/express/node_modules/connect/lib/middleware/session.js:312:9)
    at /home/student/a4/adventure-ajax-demo/node_modules/express/node_modules/connect/lib/middleware/session.js:336:9

我已将其缩小为一个问题

var bridge = new Rooms2406 ({etc...});

我不明白这里出了什么问题,就像它不知道Rooms2406是什么但我已经定义了它。

有什么想法吗?

谢谢

【问题讨论】:

  • 看起来不像 setupRoomDB 被调用:db 上的 open 事件未发出。如何创建与 MongoDB 数据库的连接?

标签: javascript node.js mongodb mongoose


【解决方案1】:

我认为问题在于,setupRoomDb 函数没有被调用,该函数负责使用模型初始化变量 room2406,因为它代表变量未定义,因此当您调用 addRooms 函数时,会发生上述错误.

没有调用 setupRoomDB 的原因仅仅是因为数据库打开事件从未被触发,因为您还没有建立数据库连接,使用 mongoose.connect 连接到您的数据库,它将解决您的错误。

更新代码

var fs = require('fs');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myDatabase')//use appropriate database here.
var db = mongoose.connection;
var Rooms2406;

var setupRoomDB = function () {
    var roomSchema = mongoose.Schema({
    title: String,
    description: String,
    roomExits: [String]
    });
    Rooms2406 = mongoose.model("Rooms2406", roomSchema);
}
db.once('open', setupRoomDB);

【讨论】:

  • 谢谢!那讲得通。但是我该怎么做才能在 mongodb 中只定义一次呢?
  • 我不确定您是否已连接到您的数据库,包括以下行 mongoose.connect('path to your db');类似 mongoose.connect('mongodb://localhost/yourdbname');
  • 完美运行!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-05-20
  • 2021-08-19
  • 1970-01-01
  • 2017-06-20
  • 2020-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多