NakedCat
NakedCat

Reputation: 860

Mongoosastic: TypeError: Cannot read property 'toLowerCase' of undefined

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?

Upvotes: 0

Views: 1464

Answers (2)

Simone
Simone

Reputation: 3996

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.

Upvotes: 1

Joe - Check out my books
Joe - Check out my books

Reputation: 16943

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

Upvotes: 0

Related Questions