【问题标题】:How come this Node.js plugin, Mongoose, doesn't work? I followed all instructions为什么这个 Node.js 插件 Mongoose 不起作用?我遵循了所有指示
【发布时间】:2011-08-13 14:46:30
【问题描述】:
mongoose.connect('mongodb://localhost/guesswho');
var Schema = mongoose.Schema;
var Message = new Schema({
    author: { type:String },
    body: { type:String },
});
var MessageModel = mongoose.model("Message");

我遵循了这里的每一条指令:https://github.com/LearnBoost/mongoose

但运行时出现此错误:Error: Schema hasn't been registered for model "Message".

我什至试过这个:var MessageModel = mongoose.model("Message", Message); 当我这样做时......没有错误。但是,MessageModel 是未定义的。

【问题讨论】:

    标签: mongodb node.js mongoose


    【解决方案1】:

    固定:

    var Message = new Schema({
        author: { type:String },
        body: { type:String },
    });
    mongoose.model("Message",Message);
    var MessageModel = mongoose.model("Message");
    var aMessage = new MessageModel();
    aMessage.author = "apple";
    aMessage.body = "orange";
    aMessage.save(function(e){
    
    });
    

    来自 IRC 的一个叫 coreb 的人修复了它。

    【讨论】:

    • 是的,您创建了架构,但从未保存过它。所以当你试图得到它时,它找不到它。
    • 出于同样的原因投票。它实际上为我节省了查找细微错误的时间。
    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多