userSchema.plugin is not a function

50 views Asked by At

I installed passport-local-mongoose package and when I used this:

const mongoose = require("mongoose");
const plm = require("passport-local-mongoose");

mongoose.connect(
  `mongodb+srv://Funn:[email protected]/`, 
   {
    dbName: "Auth"
   }

).then((c) => console.log("db connected",c.connection.host))
  .catch((e) => console.log(e));
  
   const userSchema = {
   username: String,
   password: String,
   secret:String,
  }


userSchema.plugin(plm);// because of this line it throw error 

module.exports = mongoose.model("user", userSchema); 

It throws an error:

userSchema.plugin is not a function
1

There are 1 answers

2
jQueeny On

The error is becuase userSchema is just a POJO so it doesn't have any mongoose methods inherited.

You need to create a new instance of the mongoose Schema like so:

const userSchema = new mongoose.Schema({
   username: String,
   password: String,
   secret:String,
});
userSchema.plugin(plm);