Forest Admin agent initialization issue with factory is not a function error

Feature(s) impacted

The Forest Admin agent initialization and integration with MongoDB using @forestadmin/datasource-mongoose.


Observed behavior

The Forest Admin agent fails to start and throws the following error:

Failed to initialize Forest Admin: TypeError: factory is not a function
    at D:\...\@forestadmin\datasource-customizer\dist\datasource-customizer.js:50:36
    at DecoratorsStack.applyQueuedCustomizations (D:\...\@forestadmin\datasource-customizer\dist\decorators\decorators-stack-base.js:31:23)
    at DataSourceCustomizer.getDataSource (D:\...\@forestadmin\datasource-customizer\dist\datasource-customizer.js:142:26)
    ...

This occurs after successfully connecting to MongoDB and registering models. The error seems to originate from how MongooseDatasource is added to the agent.


Expected behavior

The Forest Admin agent should start successfully after adding the MongooseDatasource and mounting on Express.js. The admin panel should be accessible at the URL provided during initialization.


Failure Logs

Your application is listening on port 3310.
Connected to MongoDB successfully.
Registered models: [ 'users' ]
Applying users collection customizations
Customizing users collection
Adding Smart Action to users collection
Adding Smart Field to users collection
Adding Smart Segment to users collection
info: Successfully mounted on Express.js
Failed to initialize Forest Admin: TypeError: factory is not a function
    at D:\...\@forestadmin\datasource-customizer\dist\datasource-customizer.js:50:36
    at DecoratorsStack.applyQueuedCustomizations (D:\...\@forestadmin\datasource-customizer\dist\decorators\decorators-stack-base.js:31:23)
    at DataSourceCustomizer.getDataSource (D:\...\@forestadmin\datasource-customizer\dist\datasource-customizer.js:142:26)
    ...

Context

  • Project name: dibsy-forest
  • Team name: dibsy
  • Environment name: Development
  • Agent technology: Node.js
  • Agent (forest package) name & version:
    • @forestadmin/agent: ^1.57.1
    • @forestadmin/datasource-mongoose: ^1.8.9
  • Database type: MongoDB
  • Recent changes made on your end if any:
    • Migrated from forest-express-mongoose to the new @forestadmin/agent architecture.
    • Implemented the following initialization logic in forestadmin.js:
      const agent = createAgent({
        authSecret: process.env.FOREST_AUTH_SECRET,
        envSecret: process.env.FOREST_ENV_SECRET,
        isProduction: process.env.NODE_ENV === 'production',
      });
      
      agent.addDataSource(new MongooseDatasource(mongoose));
      await agent.mountOnExpress(app).start();
      
    • Added customizations using customizeCollection() for the users collection.

Hello, and welcome to the community.
Iā€™m glad that you are trying out the latest agent :tada:

I believe your issue comes from the fact that you have not properly added your datasource.

You code should be

import { createMongooseDataSource } from '@forestadmin/datasource-mongoose';
...
    agent.addDataSource(
      createMongooseDataSource(mongoose)
)

Can you try that and let me know if it works for you ?
Some doc that might be helpful to your migration process:

one last remark:
You seem to be using javascript as I see in your filename forestadmin.js and stack trace,
You might want to prefer using typescript for your the new agent development environment, as you will benefit from complete typing support of all the @forestadmin/agent interfaces.
This will greatly help the migration process, because errors (like the one you are reporting), should be highlighted by the typescript checks. You will also get an improved auto-completion in your IDE

Let me know if there is anything else we can do to help with the migration :pray:

1 Like