猫鼬: 类型错误: 无法读取未定义的属性'toLowerCase'



在与Mongoosastic同步模型时,我收到以下错误:

TypeError:无法读取未定义at的属性'toLowerCase'setIndexNameIfUnset(/app/node_modules/mongoosastic/lib/mongoosastic.js: 239:29)EmbeddedDocument。schemaIndex[作为索引](/app/node_modules/mongoosastic/lib/mongoosastic.js: 385:5)EmbeddedDocument.postSave(/app/node_modules/mongoosastic/lib/mongoosastic.js:269:14) at next(/app/node_modules/kareem/index.js:198:31(/app/node_modules/kareem/index.js: 217:3)/app/node_modules/mongoose/lib/plugins/saveSubdocs.js:54:29 at each(/app/node_modules/猫鼬/lib/帮助/each.js: 11:5)模型。(/app/node_modules/猫鼬/lib/插件/saveSubdocs.js: 53:5)callMiddlewareFunction (/app/node_modules/kareem/index.js:482:23) atNext (/app/node_modules/kareem/index.js:193:9(/app/node_modules/kareem/index.js: 122:9(/app/node_modules/kareem/index.js:217:3(/app/node_modules/kareem/index.js: 307:15)/应用程序/node_modules/猫鼬/lib/model.js: 400:5/app/node_modules/mongoose/lib/model.js:324:11()

1) orderLineItem schema:

let orderLineItemSchema = new mongodb.Schema({
orderId: { type: String, es_indexed: true },
name: { type: String, es_indexed: true },
description: { type: String, es_indexed: true },
privateNotice: { type: String, es_indexed: true },
netPrice: { type: String, default: 0, es_indexed: true },
taxPercent: { type: Number, default: 23, es_indexed: true },
projectFile: { type: projectFileSchema, es_schema: projectFileSchema, es_indexed: true, es_type: 'nested', es_include_in_parent: true },
component: { type: [componentSchema], es_indexed: true, es_type: 'nested', es_include_in_parent: true },
additionalFiles: { type: [projectFileSchema], es_indexed: true, es_type: 'nested', es_include_in_parent: true },
status: { type: orderLineItemStatusSchema, es_indexed: true },
accepted: { type: Boolean, default: false, es_indexed: true },
archived: { type: Boolean, default: false, es_indexed: true }
}, {
timestamps: true
});

2) projectFileSchema:

let projectFileSchema = new mongodb.Schema({
name: { type: String, es_indexed: true },
mimeType: { type: String, es_indexed: true },
path: { type: String, es_indexed: true },
archived: { type: Boolean, default: false, es_indexed: true }
});

3.)组件模式:

let componentSchema = new mongodb.Schema({
name: { type: String, es_indexed: true },
category: { type: String, es_indexed: true },
componentId: { type: String, es_indexed: true },
symbol: { type: String, es_indexed: true },
archived: { type: Boolean, default: false, es_indexed: true }
});

4) orderLineItemStatusSchema:

let orderLineItemStatusSchema = new mongodb.Schema({
name: {
type: String,
default: 'Utworzony'
}
}, {
timestamps: true
});

5)。我的同步代码:

const synchronize = (model) => {
let stream = model.synchronize();
stream.on('data', function(err, doc){
// Logging success to the console
});
stream.on('close', function(){
// Logging ...
});
stream.on('error', function(err){
console.log(err);
});
}
module.exports = synchronize;

6)。我是这样使用它的:

const mongodb = require('mongoose');
const mtastic = require('mongoosastic');
const esClient = require('../dependencies/elasticsearch');
const orderLineItemSchema = require('../schemas/OrderLineItem/OrderLineItem');
const synchronizer = require('../helpers/synchronizer'); // This is the synchronization function
orderLineItemSchema.plugin(mtastic, {
esClient: esClient
});
let OrderLineItem = mongodb.model('OrderLineItem', orderLineItemSchema);
let interval = setInterval(() => {
synchronizer(OrderLineItem);
}, 10000);
module.exports = OrderLineItem;

这是完全相同的方式,我同步其他模型在我的应用程序,但只有这一个返回那个错误。

我该如何解决这个问题?

在mongoosastic储存库中没有解决方案就关闭了两个问题,这表明这很可能是库中的错误,可能在用户代码中没有解决方案。

这些问题和你的问题的共同点是使用了子文档,快速浏览一下库的源代码就会发现,这可能确实与对这种情况的处理不当有关。

查看库的测试套件也没有显示任何覆盖此场景的测试,进一步表明它可能根本不受支持。

从源代码中不立即清楚为什么modelName将是undefined

在任何情况下,每个mongoosastic模型构造器接受一个index参数,所以我在注册插件时声明它:

...
orderLineItemSchema.plugin(mtastic, {
esClient: esClient,
index: 'orders-index`     // <---
});
let OrderLineItem = mongodb.model('OrderLineItem', orderLineItemSchema);
...

相关内容

最新更新