【问题标题】:mongoose TypeError: Schema is not a constructormongoose TypeError: Schema 不是构造函数
【发布时间】:2017-03-09 21:53:21
【问题描述】:

我遇到了一件奇怪的事情。我有几个猫鼬模型 - 其中一个(只有一个!)我收到这个错误:

TypeError: Schema is not a constructor

我觉得这很奇怪,因为我有几个工作模式。我尝试在非工作模式中记录mongoose.Schema,它确实与我工作模式中的 mongoose.Schema 不同 - 这怎么可能?代码几乎相同。 这是非工作模式的代码:

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

var errSchema = new Schema({
  name: String,
  images:[{
    type:String
  }],
  sizes:[{
    type: String
  }],
  colors:[{
    type: Schema.ObjectId,
    ref: 'Color'
  }],
  frontColors:[{
    type: Schema.ObjectId,
    ref: 'Color'
  }],
  script: Boolean
},{
  timestamps: true
});

var Err = mongoose.model('Err', errSchema);

module.exports = Err;

工作模式的代码:

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

var colorSchema = new Schema({
  name: String,
  image: String,
  rgb: String,
  comment: String,
});

var Color = mongoose.model('Color', colorSchema);

module.exports = Color;

任何帮助将不胜感激!

【问题讨论】:

  • 当然!!伙计,我是不是觉得自己很愚蠢!感谢您的迅速回复。做出回答,我会接受的:)
  • 我遇到了同样的错误,你是怎么解决的?
  • @vashishth - 我的问题来自Schema.Types.ObjectId 中缺少的Types。一旦我添加了这个,我的问题就消失了。

标签: javascript node.js mongoose mongoose-schema


【解决方案1】:

应该是Schema.Types.ObjectId,而不是Schema.ObjectIdhttp://mongoosejs.com/docs/schematypes.html

【讨论】:

    【解决方案2】:

    我也遇到过同样的事情。我以前有这样的代码

        var mongoose = require('mongoose');
        var Schema = mongoose.Schema();
        var schema = new Schema({
            path : {type:string , required:true},
            title: {type:string , required: true}
        })
     module.export = mongoose.model('game', schema);
    

    然后我使用下面的脚本解决了构造函数问题

       var mongoose = require('mongoose');
        var schema = mongoose.Schema({
            path : {type:string , required:true},
            title: {type:string , required: true}
        })
     module.export = mongoose.model('game', schema);
    

    【讨论】:

    • 所以,这种技术也适用于我……但为什么呢?官方文档清楚地显示它用作带有new 关键字的构造函数。而且,这是简单的数据 - 不像上面的“对象”。 mongoosejs.com/docs/guide.html
    【解决方案3】:

    了解聚会迟到了,但下面的代码对我有用,可能对使用 mongoose 5.2.15 版的人有所帮助

    const mongoose = require('mongoose');
    const Scheme = mongoose.Schema;
    
    const ItemSchema = new Scheme({
        name: {
            type: String,
            require: true
        },
        date: {
            type: Date,
            default: Date.now
        }
    });
    
    module.exports = Item = mongoose.model('Item', ItemSchema);
    

    【讨论】:

      【解决方案4】:

      我已经通过导入大写的 Schema 解决了这个问题。

      上一个:

      const Scheme = mongoose.schema;
      

      修复后:

      const Schema = mongoose.Schema;
      

      完整架构:

      const mongoose = require('mongoose');
      const Schema = mongoose.Schema;
      
      const ItemSchema = new Schema({
          name : {
              type: String,
              required : true
          },
          date : {
              type : Date,
              default : Date.Now
          }
      });
      module.exports = mongoose.model('Item', ItemSchema);
      

      【讨论】:

        【解决方案5】:

        使用mongoose.Schema 而不是let Schema = mongoose.Schema();

        【讨论】:

          【解决方案6】:

          我有同样的问题

          之前的代码

          module.export = mongoose.model('AllClassList', allClassListSchema);
          

          正确的代码

          module.exports = mongoose.model('AllClassList', allClassListSchema);
          

          我在导出结束时缺少“s”

          【讨论】:

            【解决方案7】:

            在我的情况下,问题是最后应该(s)的出口:

            问题:导出中缺少(s)

            module.export = mongoose.model('Event', EventSchema);
            

            解决方案:向导出添加 (s)

            module.exports = mongoose.model('Event', EventSchema);
            

            【讨论】:

              【解决方案8】:

              对于仍然有错误的任何人

              1. 检查 package.json 上的 mongoose 版本是否为旧版本
              2. 重新安装到最新版本

              为我工作!!

              【讨论】:

                【解决方案9】:

                我遇到这个我的解决方案是在 module.export 中添加 (s),我忘了在最后添加 s,这就是为什么它告诉我我的 Schema 不是构造函数。

                【讨论】:

                  【解决方案10】:

                  问题既不是大写 Schama 也不是忘记导出“s”。 它在我编码时不小心删除了:“('猫鼬')”!!!所以我再次添加了它!问题解决了!!

                  const mongoose = require('mongoose');
                  

                  【讨论】:

                    【解决方案11】:
                    import mongoose from "mongoose";
                    
                    connectDB(mongoURI); // database connection
                    
                    // create schema 
                    
                        const student = new mongoose.Schema({
                          name: String,
                          workout: Boolean,
                          height: Number,
                        });
                        
                       // create model in students collections
                    
                        const Student = new mongoose.model("Student", student);
                      
                         // store data
                        const adder = async () => {
                       
                          const ss = await Student.create({
                            name: "jiya",
                            workout: true,
                            height: 6,
                          });
                        
                        };
                        
                    
                        //call function
                        adder();
                    

                    【讨论】:

                    • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
                    猜你喜欢
                    • 2021-04-14
                    • 1970-01-01
                    • 2017-04-08
                    • 2019-05-09
                    • 1970-01-01
                    • 2018-04-09
                    • 1970-01-01
                    • 2018-08-22
                    • 2019-08-14
                    相关资源
                    最近更新 更多