TypeError:模式._preCompile不是函数MongoDB问题

eoxn13cs  于 2022-10-22  发布在  Go
关注(0)|答案(1)|浏览(123)

当我尝试使用Mongoose方案时,我得到以下错误:我找不到解决方案。
TypeError:架构._preCompile不是函数
这就是架构的定义方式。

const mongoose = require('mongoose')
const {isEmail} = require('validator')

const userSchema= new mongoose.Schema({
  email: {
    type: String,
    require: true,
    validate: [isEmail, 'invalid email'],
    createIndexes: {unique: true},
    trim:true
  },
  nickName: 
  {
    type: String,
    require:true,
    createIndexes: {unique:true},
    trim:true
  },
  password: {
    type: String, 
    require:true,
    trim:true
  },
  dateBorn: {
    type: Date
  },
  games:[{
    type: mongoose.Schema.Types,
    ref: 'Game' 
  }],
  books:[{
    type: mongoose.Schema.Types,
    ref: 'Book' 
  }],
  movies:[{
    type: mongoose.Schema.Types,
    ref: 'Movie' //referencia a things
  }],
},{
    timestamps:true,
    versionKey:false
  })
module.exports = mongoose.model('users',userSchema)

这就是我试图导入的方式

const usersSchema = require('../models/user')

这就是我定义连接的方式

const mongoose = require('mongoose')

const dbConnect = () => {
    const DB_URI= process.env.DB_URI
    const db = mongoose.connect(DB_URI,{
      useNewUrlParser:true,
      useUnifiedTopology:true
    }, (err, res) => {
      if(!err){
        console.log('CONECTION TO MONGODB SUCCESFULL')
      }else{
        console.log('CONECTION TO MONGODB ERROR')
      }
    })
  }

module.exports = { dbConnect }

enter link description here
以下是详细信息的存储库链接

km0tfn4u

km0tfn4u1#

games:[{
    type: mongoose.Schema.Types,
  }],
  books:[{
    type: mongoose.Schema.Types,
  }],
  movies:[{
    type: mongoose.Schema.Types,
  }],

在这些行中,您必须定义要在此处存储的数据类型,比如游戏可以是String,图书可以是String/Array。你不能一直像mongoose.Schema.Types一样,认为 Mongoose 会自己选择它!!
请阅读此链接-https://mongoosejs.com/docs/schematypes.html

相关问题