【问题标题】:Creating a new collection throwing error创建一个新的集合抛出错误
【发布时间】:2013-02-13 13:49:50
【问题描述】:

以下是使用 mongoose 创建新集合时出现的错误。

  D:\Workspace\node-2012-20\node\node_modules\mongoose\lib\index.js:191
      throw new Error('Schema hasn\'t been registered for model "' + name + '"
            ^
Error: Schema hasn't been registered for model "testDocAccessModel".
Use mongoose.model(name, schema)
    at Mongoose.model (D:\Workspace\node-2012-20\node\node_modules\mongoose\lib\index.js:191)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

下面是代码

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

    exports.TestDocumentAccessSchema = new Schema({
      Id: { type: Schema.ObjectId },
      parentId: { type: Schema.ObjectId },
      userId: { type : String },
      userName: { type : String },
    });

    var testDocAccessModel = mongoose.model('TestDocumentAccess', exports.TestDocumentAccessSchema);

有什么线索吗?

【问题讨论】:

  • 在控制器中使用模型之前,请检查您的需求顺序以确保您需要模型?
  • 错误信息引用模型"testDocAccessModel",您确定您查看的代码正确吗?
  • 测试您的代码:工作正常,我认为@JamesOR 是对的...检查您的要求顺序

标签: node.js mongoose


【解决方案1】:

只需像这样更改您的代码,让我知道它是否有帮助

 var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

    var TestDocumentAccessSchema = new Schema({
      Id: { type: Schema.ObjectId },
      parentId: { type: Schema.ObjectId },
      userId: { type : String },
      userName: { type : String },
    });

    var testDocAccessModel = mongoose.model('TestDocumentAccess',TestDocumentAccessSchema); 
    exports.TestDocumentAccessSchema = TestDocumentAccessSchema;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多