类型错误:无法使用“in”运算符在未定义中搜索“复数”

问题描述 投票:0回答:3

我正在尝试使用鉴别器实现继承,如 Mongoose API 文档所示。但是,我不断收到以下错误:

C:\代码\项目 ode_modules\mongoose\lib\index.js:364 如果 (!(schema.options 中的'复数')) schema.options.pluralization = 这
^
类型错误:无法使用“in”运算符在未定义中搜索“复数” 在 Mongoose.model (C:\Code\project ode_modules\mongoose\lib\index.js:364:34)

这是导致上述错误的代码;我尝试扩展猫鼬模式类来制作我的基本模式:

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

function ResourceSchema() {
  Schema.apply(this, arguments);

  this.add({
    title: String
  });
}
util.inherits(ResourceSchema, Schema);

module.exports = mongoose.model('Resource', ResourceSchema);

我也尝试在最后一行设置集合名称,但这没有帮助。

module.exports = mongoose.model('Resource', ResourceSchema, 'resources');
node.js mongodb mongoose
3个回答
7
投票

您正在尝试根据模式创建模型,而不是模式实例

用这个代替:

module.exports = mongoose.model('Resource', new ResourceSchema());

这类似于您通常创建模型的方式:

var schema = new Schema({ ... }); // or, in your case, ResourceSchema
var model  = mongoose.model('Model', schema);

0
投票

在您的

userSchema.js
文件中:

const mongoose = require('mongoose');

const userSchema=new mongoose.Schema({
    name:{
        type: String,
        required : true
    },
    email:{
        type: String,
        required : true
    },
    password:{
        type: String,
        required : true
    },
})

const User=mongoose.model('USER',userSchema)
module.exports =User

0
投票

我现在遇到同样的错误

D:\Bakend\mega_projects ode_modules\mongoose\lib\mongoose.js:609 if (schema == null || !(schema.options 中的'复数')) { ^

TypeError:无法使用“in”运算符在未定义中搜索“复数” 在 Mongoose._model (D:\Bakend\mega_projects ode_modules\mongoose\lib\mongoose.js:609:43) 在 Mongoose.model (D:\Bakend\mega_projects ode_modules\mongoose\lib\mongoose.js:578:27) 在对象。 (D:\Bakend\mega_projects\Models\Profile.js:25:27) 在 Module._compile (节点:内部/模块/cjs/loader:1376:14) 在 Module._extensions..js (节点:内部/模块/cjs/loader:1435:10) 在Module.load(节点:内部/模块/cjs/loader:1207:32) 在 Module._load (节点:内部/模块/cjs/loader:1023:12) 在 Module.require (节点:内部/模块/cjs/loader:1235:19) 在需要时(节点:内部/模块/助手:176:18) 在对象。 (D:\Bakend\mega_projects\Controllers\Profile.js:1:17)

Node.js v20.10.0 [nodemon] 应用程序崩溃 - 启动前等待文件更改...

在此输入图片描述

© www.soinside.com 2019 - 2024. All rights reserved.