Why am I getting this error: MissingSchemaError: Schema hasn't been registered for model "Business"

65 views Asked by At

This is the error I'm getting: "MissingSchemaError: Schema hasn't been registered for model "Business"."

I'm using AstraDB vector database. I made a function to initialize a Mongoose model:

export const initMongooseBusinessModel = async () => {
    const Business = mongoose.model(
      "Business",
      new mongoose.Schema(
        {
          business_id: String,
          $vector: {
            type: [Number],
            validate: (vector) => vector && vector.length === 1536,
          },
        },
        {
          collectionOptions: {
            vector: {
              size: 1536,
              function: "cosine",
            },
          },
        },
      ),
    );
    await Business.init();
    console.log("complete")
};

I am trying to use the model in a function of another file:

import mongoose from "mongoose"

export const addToAstraDB = async (url) => {
    try {
        const Business = mongoose.model("Business")
        const businessURL = url
        // Rest of the code goes here...

I'm getting this error:

1

There are 1 answers

0
Aaron On BEST ANSWER

Val Karpov (the creator of Mongoose) recently posted a writeup on building a RAG app using Mongoose and Astra DB for Vector Search. In fact, looking at the Git repo behind Val's article, suggests that you could create a file called Business.js that looks like this:

'use strict';

const mongoose = require('../mongoose');

const businessSchema = new mongoose.Schema({
    business_id: String,
    $vector: [Number]
});

module.exports = mongoose.model('Business', businessSchema);

And then pull it in from your other file instead, like this:

import mongoose from "mongoose"

export const addToAstraDB = async (url) => {
    try {
        const Business = require('./Business.js');
        const businessURL = url;
        // Rest of the code goes here...

Although, in the article itself, it looks like he recommends just doing this in the second (other) file:

const businessSchema = new mongoose.Schema({
    business_id: String,
    $vector: [Number]
});
const Business = mongoose.model('Business', businessSchema);

Give Val's article and repo a read through, and that should point you in the right direction.