在 AdonisJS 提供程序中打开 Mongoose 连接超时

Opening Mongoose connection in AdonisJS provider times out

我正在关注 this article 在 AdonisJS 5 项目中使用 Mongo。
我有一个由 node ace make:provider Mongo 创建的 AdonisJS 提供程序(它在 .adonisrc.json 中注册):

import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import { Mongoose } from 'mongoose'

export default class MongoProvider {
  constructor(protected app: ApplicationContract) {}

  public async register() {
    // Register your own bindings
    const mongoose = new Mongoose()

    // Connect the instance to DB
    await mongoose.connect('mongodb://docker_mongo:27017/mydb')

    // Attach it to IOC container as singleton
    this.app.container.singleton('Mongoose', () => mongoose)
  }

  public async boot() {
    // All bindings are ready, feel free to use them
  }

  public async ready() {
    // App is ready
  }

  public async shutdown() {
    // Cleanup, since app is going down
    // Going to take the Mongoose singleton from container
    // and call disconnect() on it
    // which tells Mongoose to gracefully disconnect from MongoBD server
    await this.app.container.use('Mongoose').disconnect()
  }
}

我的模型是:

import { Schema, model } from '@ioc:Mongoose'

// Document interface
interface User {
  email: string
}

// Schema
export default model(
  'User',
  new Schema<User>({
    email: String,
  })
)

控制器:

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import User from 'App/Models/User'

export default class UsersController {
  public async index({}: HttpContextContract) {
    // Create a cat with random name
    const cat = new User({
      email: Math.random().toString(36).substring(7),
    })
    // Save cat to DB
    await cat.save()

    // Return list of all saved cats
    const cats = await User.find()

    // Return all the cats (including the new one)
    return cats
  }
}

并且正在超时。
它正在工作,当我像这样在控制器中打开连接时:

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import User from 'App/Models/User'
import mongoose from 'mongoose'

export default class UsersController {
  public async index({}: HttpContextContract) {
    await mongoose.connect('mongodb://docker_mongo:27017/mydb')

    // Create a cat with random name
    const cat = new User({
      email: Math.random().toString(36).substring(7),
    })
    // Save cat to DB
    await cat.save()

    // Return list of all saved cats
    const cats = await User.find()

    // Close the connection
    await mongoose.connection.close()

    // Return all the cats (including the new one)
    return cats
  }
}

我刚刚创建了一个 AdonisJS 提供程序,在 .adonisrc.json 中注册了它,创建了一个带有类型的 contracts/Mongoose.ts,并在控制器中使用了该模型。

有什么想法吗?我被这个卡住了一天。
谢谢

如果有人感兴趣:

在文章作者的帮助下,创建模型时缺少 Mongoose 无法正常工作的原因(Mongoose.model 而不仅仅是 model:

export default Mongoose.model(
  'User',
  new Schema<User>({
    email: String,
  })
)

我设法通过不将 mongoose 存储在变量中来解决这个问题。您在 MongoProvider 中声明的 mongoose 变量似乎是超时错误的根源。

所以我做了如下:

export default class MongoProvider {
      constructor(protected app: ApplicationContract) {}
    
      public async register() {
        await mongoose.connect('mongodb://localhost:27017/dbName')
    
        this.app.container.singleton('Mongoose', () => mongoose)
      }
    
      public async boot() {
        // All bindings are ready, feel free to use them
      }
    
      public async ready() {
        // App is ready
      }
    
      public async shutdown() {
        await this.app.container.use('Mongoose').disconnect()
      }
}