用户.create不是函数mongodb

rqenqsqc  于 2022-11-22  发布在  Go
关注(0)|答案(3)|浏览(139)
const express = require("express")
const app = express()
const cors = require("cors")
const mongoose = require("mongoose")
const User = "./models/user.model"
app.use(cors())
app.use(express.json())
mongoose.connect("mongodb://localhost:27017/full-mern-stack")

app.get("/",(req,res)=>{
    res.send("home page")
})
app.get("/api/register",(req,res)=>{
    res.send("Correct Route")
})
app.post("/api/register", async (req,res)=> {
   try{
    await User.create({
        name:req.body.name,
        email:req.body.email,
        password:req.body.password,
    })
    res.json({status:"ok"})
   }catch(err){
       console.log(err)
       res.json({status:"error",error:"Duplicate email"})

   }

})
app.post("/api/login", async (req,res)=> {
    
    const user =  await User.findOne({
         email:req.body.email,
         password:req.body.password,
     })
     if (user) {
         return res.json({status:"ok",user:true})
     } else{
         return res.json({status:"error",user:false})
     }
     res.json({status:"ok"})
  
 
 })



app.listen(3002,()=>{
    console.log("listening")
})

im getting this error :TypeError: User.create is not a function at C:\Users\joshm\Downloads\New folder (12)\myapp\Server\Server.js:18:16 at Layer.handle [as handle_request] (C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\express\lib\router\layer.js:95:5) at C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\express\lib\router\index.js:341:12) at next (C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\express\lib\router\index.js:275:10) at C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\body-parser\lib\read.js:130:5 at invokeCallback (C:\Users\joshm\Downloads\New folder (12)\myapp\node_modules\raw-body\index.js:224:16)
model :

const mongoose = require("mongoose")

const User = new mongoose.Schema({
    name:{type:String,required:true},
    email:{type:String,required:true,unique:true},
    name:{type:String,required:true},
    quote:{type:String},
},
{collection:"user-data"}

)

const model = mongoose.model("UserData",User)

module.exports = model
ijxebb2r

ijxebb2r1#

您以错误的方式要求模型,请将const User = "./models/user.model"更改为const User = require("./models/model")
您在模型文件中导出的内容。

x33g5p2x

x33g5p2x2#

您可以通过以下方式更改User.js,

const mongoose = require('mongoose');
const { Schema } = mongoose;
const model = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    quote: { type: String },
}, 
{ collection: 'test1' }
);
const User = mongoose.model("UserData",model)
module.exports = User;

然后在index.js文件中这样导入

const mongoose = require('mongoose');
const User = require('./models/User');
e0bqpujr

e0bqpujr3#

用户模型

const mongoose = require("mongoose");
    const Schema = mongoose.Schema;

    const userSchema =  new Schema({
      name:{type:String,required:true},
      email:{type:String,required:true,unique:true},
      password:{type:String, require:true}
      quote:{type:String},
     },
     {collection:"user-data"}
     )

    module.exports = mongoose.model("User",userSchema)

美国石油学会

const User = require("./models/user.model")
  app.post("/api/register", async (req,res)=> {
   try{
    const data = {
      name: req.body.name,
      email: req.body.email,
      password: req.body.password
    }
    await  User.create(data)
    res.json({status:"ok"})
   }catch(err){
       console.log(err)
       res.json({status:"error",error:"Duplicate email"})
   }
})

如果上述路由创建功能不起作用,则使用此功能

const user = new User();
   await user.save({
        name:req.body.name,
        email:req.body.email,
        password:req.body.password,
    });

在这里,我们创建一个示例,然后保存在数据库中。

相关问题