skip to Main Content

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:

2

Answers


  1. That is not how you initialise mongoose models. You need to export the model from your module like so:

    Business.js

    import mongoose from "mongoose"
    const businessSchema = new mongoose.Schema(
            {
              business_id: String,
              $vector: {
                type: [Number],
                validate: (vector) => vector && vector.length === 1536,
              },
            },
            {
              collectionOptions: {
                vector: {
                  size: 1536,
                  function: "cosine",
                },
              },
            },
        );
    const Business = mongoose.model("Business", businessSchema);
    export default Business; 
    

    OtherFile.js

    import mongoose from "mongoose"
    import Business from "./Business.js"
    export const addToAstraDB = async (url) => {
        try { 
           const doc = await Business.find(); //< Use the model that you imported
        }
        //...
    };
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search