0
votes

While syncrhonizing models with Mongoosastic I am receiving this error:

TypeError: Cannot read property 'toLowerCase' of undefined at setIndexNameIfUnset (/app/node_modules/mongoosastic/lib/mongoosastic.js:239:29) at EmbeddedDocument.schemaIndex [as index] (/app/node_modules/mongoosastic/lib/mongoosastic.js:385:5) at EmbeddedDocument.postSave (/app/node_modules/mongoosastic/lib/mongoosastic.js:269:14) at next (/app/node_modules/kareem/index.js:198:31) at Kareem.execPost (/app/node_modules/kareem/index.js:217:3) at /app/node_modules/mongoose/lib/plugins/saveSubdocs.js:54:29 at each (/app/node_modules/mongoose/lib/helpers/each.js:11:5) at model. (/app/node_modules/mongoose/lib/plugins/saveSubdocs.js:53:5) at callMiddlewareFunction (/app/node_modules/kareem/index.js:482:23) at next (/app/node_modules/kareem/index.js:193:9) at next (/app/node_modules/kareem/index.js:212:9) at Kareem.execPost (/app/node_modules/kareem/index.js:217:3) at _cb (/app/node_modules/kareem/index.js:307:15) at /app/node_modules/mongoose/lib/model.js:400:5 at /app/node_modules/mongoose/lib/model.js:324:11 at runMicrotasks ()

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.) component schema:

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.) My synchronization code:

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.) Here's how I use it:

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;

It is the exact same way I synchronize other models in my app but only this one returns that error.

How can I fix this?

2
Can you get a "fuller" stack trace?Joe - Elasticsearch Handbook
@JoeSorocin Sadly I don't have the full stack trace right now because after I deleted everything from my MongoDB and then restarted everything this error stopped happening. That obviously isn't the solution, I just needed the app to work properly for presentation purposes.SphinxWar
I hear ya but we'll need more detail to debug this.Joe - Elasticsearch Handbook
@JoeSorocin I managed to get the same error. Now I edited the question with the full stack trace Node is giving me.SphinxWar
Can you also show what orderLineItemSchema looks like please?Joe - Elasticsearch Handbook

2 Answers

1
votes

There are two issues closed without a solution in the mongoosastic repository, suggesting that this is very likely a bug in the library that may not have a solution in user code.

What those issues and yours have in common is the use of subdocuments, and a quick glance at the library's source code suggests that it may be indeed related to an improper handling of that case.

A look at the test suite of the library also does not show any tests covering this scenario, further suggesting that it may simply not be supported.

0
votes

It's not immediately clear from the source code why the modelName would be undefined...

In any event, each mongoosastic model constructor accepts an index parameter so I'd declare it when I'm registering the plugin:

...
orderLineItemSchema.plugin(mtastic, {
    esClient: esClient,
    index: 'orders-index`     // <---
});

let OrderLineItem = mongodb.model('OrderLineItem', orderLineItemSchema);
...