gpt4 book ai didi

node.js - 类型错误 : Cannot use 'in' operator to search for 'pluralization' in undefined

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:46 26 4
gpt4 key购买 nike

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

C:\Code\project\node_modules\mongoose\lib\index.js:364 if (!('pluralization' in schema.options)) schema.options.pluralization = thi
^
TypeError: Cannot use 'in' operator to search for 'pluralization' in undefined at Mongoose.model (C:\Code\project\node_modules\mongoose\lib\index.js:364:34)

这是导致上述错误的代码;我尝试扩展 mongoose Schema 类来创建我的基本架构:

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');

最佳答案

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

改用这个:

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

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

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

关于node.js - 类型错误 : Cannot use 'in' operator to search for 'pluralization' in undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31570288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com