运行应用程序时出现此错误 我尝试更改一些内容,但实际上没有任何作用,并且应用程序崩溃了并且无法运行。
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
at new MissingSchemaError (C:\Users\SMIT\Desktop\project\node_modules\mongoose\lib\error\missingSchema.js:20:11)
at Mongoose.model (C:\Users\SMIT\Desktop\project\node_modules\mongoose\lib\index.js:378:13)
at Object.<anonymous> (C:\Users\SMIT\Desktop\project\model\User.js:21:30)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\SMIT\Desktop\project\route\api\user.js:4:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
[nodemon] app crashed - waiting for file changes before starting...
这是主文件 C:\ Users \ SMIT \ Desktop \ project \ index.js
const express= require('express');
const mongoose=require('mongoose');
const bodyParser=require('body-parser');
const user=require('./route/api/user');
const userprofile=require('./route/api/userprofile');
const app=express();
//body parser middleware
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//database confid into variable using variable name that I used in key.js of config folder
const database=require('./config/key').mongoURI;
//connecting to mongodb
//.then will print connected if successful
//catch will print error if not
mongoose.connect(database).then(()=>console.log("connected")).catch(err=>console.log(err));
//will give this output if successful
app.get('/',(req,res)=>res.send("testing!123"));
//gave port number 3000 input to run
//using route to check the output of user.js and userprofile.sj
app.use('/api/user',user);
app.use('/api/userprofile',userprofile);
//will run at 3000 port on localhost
const port=process.env.PORT || 3000;
//it will print this in the terminal after npm start or npm run server
//this will print the consr port in conlsole
app.listen(port,()=> console.log('port is:: ${port}'));
C:\ Users \ SMIT \ Desktop \ project \ model / User.js
const mongoose=require('mongoose');
const Schema=mongoose.Schema;
//creating schema for users
const UserSchema=new mongoose.Schema({
name:{
type:String,
required:true
},
password:{
type:String,
required:true
},
email:{
type:String,
required:true
}
});
module.exports=user=mongoose.model('User','UserSchema');
C:\ Users \ SMIT \ Desktop \ project \ route \ api \ user.js
const express=require('express');
const router=express.Router();
const bcrypt=require('bcryptjs');
const user=require('../../model/User');
router.get('/demo',(req,res)=>res.json({output:"user"}));
//creating route for regsiteration
router.post('/register',(req,res)=>{
//find if email exists or not.
user.findOne({email: req.body.email}).then(user=>{
if(user){
return res.status(400).json({email:"email is already registered"});
}
else{
//else will creaete new user
const newUser=new User({
name: req.body.name,
email:req.body.email,
password:req.body.password
})
//generate salt and hash pass with salt
bcrypt.genSalt(10,()=>{
//hashing password
bcrypt.hash(newuser.password, salt, (err,hash)=>{
if(err)
throw err;
newUser.password=hash;
newUser.save()
.then(user=>res.json())
})
})
}
})
});
module.exports=router;
答案 0 :(得分:2)
在文件“ C:\ Users \ SMIT \ Desktop \ project \ model / User.js ”中的导出变量
var User = mongoose.model('User',UserSchema);
module.exports = {User};
希望它将解决此问题