Next.js Discord

Discord Forum

Mongoose with out api

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
Is it possible to send data to MongoDB with mongoose with out a api ?
I know redis will work but I want to know if I can use mongoose for it !


  await redis.hincrby(key, JSON.stringify(event), 1)



import AnalyticsModel  from "@/models/analutics";
import { connectMongoDB } from "@/lib/database/mongodb/DBConnection";

export class Analytics {
    async track( event: object = {}) {
        try {
        await connectMongoDB();
             // Create a new document using the AnalyticsModel
            const analyticsData = new AnalyticsModel(event);

            // Save the analytics data to the database
            await analyticsData.save();
        console.log(event)
        console.log('Analytics data saved successfully');
        } catch (error) {
            // console.error('Error occurred:', error);
            // throw error;
        }
    }
}


export const analytics = new Analytics()

Code above sadly dont work getting the following error:
Error connecting to MongoDB: [TypeError: mongooseWEBPACK_IMPORTED_MODULE0default(...).connect is not a function]

I tested the event: object = {} and it keep reciving information now I only need it to send to mongoDB

0 Replies