mongoose TypeError:Schema不是构造函数 [英] mongoose TypeError: Schema is not a constructor

查看:219
本文介绍了mongoose TypeError:Schema不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've encountered a strange thing. I have several mongoose models - and in one of them (only in one!) I get this error:

TypeError: Schema is not a constructor

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

I find it very strange as I have several working schemas. I tried logging mongoose.Schema in the non-working schema and it is indeed different from the mongoose.Schema in my working schemas - how is that possible? The code is almost identical. Here's the code for the non-working 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;

任何帮助都将不胜感激!

Any help would be appreciated!

推荐答案

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

It should be Schema.Types.ObjectId, not Schema.ObjectId: http://mongoosejs.com/docs/schematypes.html

这篇关于mongoose TypeError:Schema不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆