How to make mongoose work with Cloudflare Pages?

Hi, i’m building app with astro and @astrojs/cloudflare adapter for SSR.

I got an error:
[ERROR] TypeError: Cannot read properties of undefined (reading 'Result')

const { Schema, model, models } = mongoose;
const resultSchema = new Schema({
  date: {
    type: Date,
    required: true
  moodDimensions: {
    type: [moodDimensionSchema],
    required: true
const Result = models.Result || model("Result", resultSchema); // HERE IS THE SOURCE OF ERROR (models.Result)

Everything is working fine when npm run dev, but it is not working on Cloudflare or if i run locally with wrangler wrangler pages dev ./dist

Please help me to fix this. What is the root of the problem?

Hi @skorphil,

The Workers Team are more active on their Discord server, so as you have not received a response here, I would recommend asking on Discord if you haven’t already.

Please do feel free to update the thread here if you get a resolution, to help others who may find this topic.