skip to Main Content

In db.js

import mongoose from "mongoose";



export function dbConnection() {
  try {
    mongoose.connect("mongodb://0.0.0.0:27017", {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    console.log("DB connected successfully ");
  } catch (error) {
    console.log("Not connected ");
  }
}

In Jobmodel.js

import mongoose from "mongoose";

import autoIncrement from "mongoose-auto-increment";


const JobSchema = new mongoose.Schema({
    company: {
        type: String,
        required: true
    },
    new: {
        type: Boolean
    },
    position: {
        type: String,
        required: true
    },
    role: {
        type: String,
        required: true,
    },
    postedAt: {
        type: String,
    }
});

// Apply the auto-increment plugin to your schema
JobSchema.plugin(autoIncrement.plugin, {
    model: 'Job',
    field: 'id', // The field to auto-increment
    startAt: 1, // Start from 1
    incrementBy: 1 // Increment by 1
});

const JobModel = mongoose.model('Job', JobSchema);


export default JobModel;

And in index.js main file i have called the dbConnection() function

Then Why am i getting this error in the console?

Note: I have also tried to initialize the autoincrement library before mongoose.connect in db.js file. Like this => autoIncrement.initialize(mongoose.connection);

 if (!counterSchema || !IdentityCounter) throw new Error("mongoose-auto-increment has not 
been initialized");
                                                ^

Error: mongoose-auto-increment has not been initialized
    at exports.plugin (G:REACTFRONTEND MENTOR IOJob Listingapinode_modulesmongoose-auto-incrementindex.js:36:49)
    at Schema.plugin (G:REACTFRONTEND MENTOR IOJob Listingapinode_modulesmongooselibschema.js:1932:3)
    at file:///G:/REACT/FRONTEND%20MENTOR%20IO/Job%20Listing/api/model/Jobmodel.js:28:11   
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

2

Answers


  1. Chosen as BEST ANSWER

    I have also tried this approach

    import mongoose from "mongoose";
    import autoIncrement from "mongoose-auto-increment";
    
    
    autoIncrement.initialize(mongoose.connection);
    
    export function dbConnection() {
      try {
        mongoose.connect("mongodb://0.0.0.0:27017", {
          useNewUrlParser: true,
          useUnifiedTopology: true,
        });
        console.log("DB connected successfully ");
      } catch (error) {
        console.log("Not connected ");
      }
    } 
    

    Then i get this error

     throw new MongooseError('Model.findOne() no longer accepts a callback');
              ^
    MongooseError: Model.findOne() no longer accepts a callback
    

  2. You need to initialize the mongoose-auto-increment as the error shows.

    import mongoose from "mongoose";
    import autoIncrement from "mongoose-auto-increment";
    
    
    export function dbConnection() {
      try {
        mongoose.connect("mongodb://0.0.0.0:27017", {
          useNewUrlParser: true,
          useUnifiedTopology: true,
        });
        // init autoIncrement ✅
        autoIncrement.initialize(mongoose.connection);
        console.log("DB connected successfully ");
      } catch (error) {
        console.log("Not connected ");
      }
    }
    

    refs

    https://www.npmjs.com/package/mongoose-auto-increment

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search