mongoose userSchema.plugin不是函数

t5zmwmid  于 11个月前  发布在  Go
关注(0)|答案(1)|浏览(70)

我安装了passport-local-mongoose包,当我使用这个:

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);

字符串
它抛出一个错误:

userSchema.plugin is not a function

6ovsh4lw

6ovsh4lw1#

这个错误是因为userSchema只是一个POJO,所以它没有继承任何mongoose方法。
你需要创建一个mongoose Schema的新示例,如下所示:

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

字符串

相关问题