创建一个新的集合抛出错误

时间:2012-12-19 05:38:25

标签: node.js mongoose

以下是使用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);

任何线索?

1 个答案:

答案 0 :(得分:0)

只需更改您的代码,让我知道它是否有用

 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;