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
That is not how you initialise mongoose models. You need to export the model from your module like so:
Business.js
OtherFile.js
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:And then pull it in from your other file instead, like this:
Although, in the article itself, it looks like he recommends just doing this in the second (other) file:
Give Val’s article and repo a read through, and that should point you in the right direction.